home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-05-17 | 90.4 KB | 3,662 lines | [TEXT/MPS ] |
- /*
- File: FWiXMain.c
-
- Contains: Application software to transfer files over FireWire.
-
- Version: 1.0
-
- Written by: Jay Lloyd
-
- Copyright: © 1996-1999 by Apple Computer, Inc., all rights reserved.
-
- File Ownership:
-
- DRI: Jay Lloyd
-
- Other Contact:
-
- Technology: FireWire
-
- Writers:
-
- (DCB) Clinton Bauder
- (jkl) Jay Lloyd
-
- Change History (most recent first):
-
- <FW38> 4/8/99 jkl More interface cleanup.
- <FW37> 2/2/99 DCB Fix for latest MI headers.
- <FW36> 12/19/98 DCB More cleanup for SDK.
- <FW35> 7/24/98 jkl Removed page alignment of data buffers.
- <FW34> 7/22/98 jkl Call CreateMenus before InitPrefs. InitPrefs tries to adjust a
- menu item name.
- <33> 1/15/98 jkl Update for new headers.
- <FW32> 6/19/97 jkl Added support for GetNodeList and SendItems AppleScript
- commands. Saved the icons view as a preference. Updated the open
- sharing setup command for Tempo to open File Sharing .
- <FW31> 6/10/97 jkl Made sure a device added message does not result in the same
- device getting added twice.
- <FW30> 5/28/97 jkl Made sure send or receive error is cleared before calling error
- dialog. The not cleared error could cause the dialog to
- repeatedly display itself.
- <FW29> 5/27/97 jkl Changed memory allocation for data buffers to ensure they are on
- 4k boundaries.
- <FW28> 5/16/97 jkl Corrected some GetPort SetPort bracketing around updates and
- drawing.
- <FW27> 5/15/97 jkl Modified error handling routines to recover from errors during
- the transfer check before file sending has started. Held all of
- the completion routines.
- <FW26> 5/7/97 jkl Modified linked lists to double linked lists to simplify
- insertion and deletion of list items.
- <FW25> 4/29/97 jkl Fixed main symbol export. Made sure drop folder exsists before
- calling open drop folder routine.
- <FW24> 4/29/97 jkl Added global alert routine to handle alert event filters.
- Corrected a problem that could leave the connected fwix machine
- list corrupted.
- <FW23> 4/8/97 jkl Added page up/down, home, and end key scrolling support. Changed
- my BBS initials to not conflict with Jim Luther's.
- <FW22> 3/18/97 jkl Added new icons. Changed text drawing to use TETextBox. Moved
- prefs related routines to prefs.c.
- <FW21> 2/27/97 jkl Modified preferences to be under receiver control and removed
- prompt preference. Added Edit menu and moved Preferences command
- to there. Added support for scrolling the sender window.
- <FW20> 2/21/97 jkl Changed icon placement and management in sender window for
- better multiple node support. Updated preferences menu to handle
- menus and sound popup correctly.
- <FW19> 2/19/97 jkl Added Reply/Request options to SendFWXInfo. Set the state of the
- sound notification popup menu according to the play sound check
- box. Added play the sound after it is selected in the popup.
- Corrected window zoom states. Made sure notification resources
- are not purgeable.
- <FW18> 2/13/97 jkl Added a ForkWriteComplete to make sure a data buffer is
- available before starting to read a new file fork.
- <FW17> 2/11/97 jkl Added read control and read data buffers and queues. Moved fwix
- preferences to a file and closed file in all cases. Fixed
- buttons in no sharing name dialog.
- <FW16> 2/10/97 ES Cleaned up deallocation code. Removed system heap allocations.
- <FW15> 2/7/97 ES Added call to GetNextFWXClientEvent in event loop.
- <FW14> 2/6/97 ES Changed to maintain list of all nodes that have been opened.
- Will send quit notification to and close all nodes that have
- been opened when quitting.
- <FW13> 2/5/97 ES Changed a CloseFWXDriver to CloseFWXNode.
- <FW12> 2/2/97 jkl Added Open Sharing Setup command to no machine name alert.
- Modified menus and corrected menu state enabled/disabled
- problems. Changed open drop folder command to make the finder
- the foreground process. Corrected view size menu commands to
- only work if that size is not the current size. Move the
- preferences to a file. Correct the flahsing icon notification.
- Moved NewPtrSys calls that are safe back to NewPtr.
- <FW11> 1/29/97 ES Changed InstallCompletionRoutineProcs to set up completion
- routines to return status in D0. This is a workaround for a bug
- in the File Manager.
- <FW10> 1/27/97 ES Added creation of FWControl PB queue.
- <FW9> 1/27/97 jkl Added timer routines to check for transfer disconnects and
- errors. At idle check for timeout errors. Cleaned up from a
- device removed event properly if a transfer is in progress.
- Removed killio routine from CloseWindow, will let closing the
- node handle it. Added check for transmit in progress and option
- to quit or continue. Fixed stop button in progress dialog by
- adding setport call. Modified view options menu to show check
- marks instead of toggle title.
- <FW8> 1/17/97 ES Changed a CallFWXDriver to CallFWXNode. Changed NewPtrs to
- NewPtrSyss because some buffers need to be held to be VM safe;
- this is overkill, and really we just need to hold the right
- buffers. Used NewPtrSysClear to allocate pRecvNode so that its
- next pointer is nil.
- <FW7> 1/16/97 jkl Removed an open DebugStr call.
- <FW6> 1/16/97 jkl Added user interface features for alpha candidate. Added a
- preferences dialog, changed window behavior to match normal
- window behavior, changed fwix node icons to only be displayed if
- the node has fwix running. Added receive file notification.
- <FW5> 1/8/97 ES Changed to use FWX nodes instead of FWX drivers. Fixed up hot
- plugging and unplugging.
- <FW4> 11/13/96 jkl Moved from DoDriverIO interface to CallDriver. Implemented stop
- transfer. Now Indentify receivers by name.
- <FW3> 10/31/96 jkl Fixed event handling when copy progress dialog
- is being displayed. Added routine to update copy
- progress dialog at idle time. Moved receive folder
- check and initialization to application init time.
- <FW2> 10/16/96 jkl Modified queue handling to use OS Utils interrupt
- safe queue routines. Created File Write Completion
- routine proc. Removed reads and writes from idle
- handler. The idle handler only checks to see if
- the read or write is done. The reads and writes
- are continued in the various completion routines.
- <FW1> 10/2/96 jkl Initial check-in.
- */
-
- #include <QuickDraw.h>
- #include <SegLoad.h>
- #include <Types.h>
- #include <Memory.h>
- #include <Resources.h>
- #include <Fonts.h>
- #include <Controls.h>
- #if !ETO_Build
- #include <ControlDefinitions.h>
- #endif
- #include <Menus.h>
- #include <Dialogs.h>
- #include <Events.h>
- #include <AppleEvents.h>
- #include <Processes.h>
- #include <Windows.h>
- #include <Errors.h>
- #include <Files.h>
- #include <Folders.h>
- #include <Script.h>
- #include <Devices.h>
- #include <TextUtils.h>
- #include <ToolUtils.h>
- #include <StandardFile.h>
- #include <Icons.h>
- #include <DiskInit.h>
- #include <Notification.h>
- #include <Timer.h>
- #include <Sound.h>
-
- #include "FWiX.h"
-
- #include "FWiXmain.h"
- #include "FWiXdrag.h"
-
- #include <stdio.h>
- char debugStr[256];
- static pascal void FWDebugStr(
- ConstStr255Param debuggerMsg)
- {
- #ifdef FW_DEBUG_BUILD
- #if FW_DEBUG_BUILD
- DebugStr (debuggerMsg);
- #endif
- #endif
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // Internal procedure prototypes.
- //
-
- static OSErr SendDeviceAddedToSelf (
- FWXNodeID theNodeID);
-
- static OSErr SendDeviceRemovedToSelf (
- FWXNodeID theNodeID);
-
- static pascal OSErr HandleDeviceAddedEvent (
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- SInt32 handlerRefcon);
-
- static OpenNodePtr GetNodeInfoFromID (
- FWXNodeID nodeID);
-
- static pascal OSErr HandleDeviceRemovedEvent (
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- SInt32 handlerRefcon);
-
- void AdjustNodeIcons (
- WindowDataPtr pWinData,
- Rect *windRect);
-
- OSErr SendFWXInfo (
- FWXNodeID nodeID,
- UInt32 sendType);
-
- OSErr SendFWXQuit (void);
-
- static OSErr SetupFWXRead (
- FWXNodeID nodeID);
-
- static OSErr GetNodeIDFromEvent (
- AppleEvent *pEvent,
- FWXNodeID *nodeID);
-
- static OSErr GetNodeIDFromName (
- FWXNodeID *nodeID,
- ConstStr255Param nodeName);
-
- static OSErr NewSenderWindow (void);
-
- static void GetDisplayRect (
- Rect *r,
- WindowPtr pWin,
- WindowDataPtr pWinData);
-
- OSErr AdjustScrollBars (
- WindowPtr pWin,
- Boolean adjustSizes);
-
- OSErr InitRecvNode (
- FWXNodeID nodeID,
- ConstStr255Param nodeName);
-
- static pascal void HandleReplyTimeout (
- MyTMTaskPtr pTMTask);
-
- static pascal OSErr HandleGetNodeListEvent (
- AppleEvent *pAppleEvent,
- AppleEvent *pReplyEvent,
- SInt32 refCon);
-
- static pascal OSErr HandleSendItemsEvent (
- AppleEvent *pAppleEvent,
- AppleEvent *pReplyEvent,
- SInt32 refCon);
-
- static OSErr GetFSItemsFromEvent (
- FSSpecPtr *pSendItems,
- AppleEvent *pEvent,
- UInt16 *numItems);
-
- static OSErr GetReceiveNodesFromEvent (
- FWXNodeID **pReceiveNodeIDList,
- AppleEvent *pAppleEvent,
- UInt16 *numNodes);
-
- static pascal OSErr HandleOpenApplicationEvent (
- AppleEvent *pAppleEvent,
- AppleEvent *pReplyEvent,
- SInt32 refCon);
-
- static pascal OSErr HandleOpenDocumentsEvent (
- AppleEvent *pAppleEvent,
- AppleEvent *pReplyEvent,
- SInt32 refCon);
-
- static pascal OSErr HandleQuitApplicationEvent (
- AppleEvent *pAppleEvent,
- AppleEvent *pReplyEvent,
- SInt32 refCon);
-
- static OSErr InstallAppleEventHandlers (void);
-
- static OSErr InstallCompletionRoutineProcs (void);
-
- static void DrawWindow (
- WindowPtr pWin);
-
- static OSErr CreateMenus (void);
-
- static void HandleCloseWindow (
- WindowPtr pWin);
-
- static void HandleQuit (void);
-
- static void HandleMenuCommand (
- SInt32 menuVal);
-
- OSErr OpenDropFolder(void);
-
- static OSErr OpenSharingSetup(void);
-
- static OSErr HandleViewSize(
- SInt16 menuItem);
-
- static void HandleMouseDownEvent (
- EventRecord *pEventRecord);
-
- static pascal void HandleThumbScroll (
- ControlHandle scrollCtl,
- WindowPtr pWin,
- Point mouse);
-
- static pascal void HandleScrollAction (
- ControlHandle scrollCtl,
- SInt16 part);
-
- static void HandleGrowWindow(
- WindowPtr pWin,
- Point clickLoc);
-
- static void HandleZoomWindow(
- WindowPtr pWin,
- SInt16 whichZoom);
-
- void StopTransfer (
- RecvNodePtr pNode);
-
- static void HandleKeyEvent (
- EventRecord *pEventRecord);
-
- static void HandleOSEvent (
- EventRecord *pEventRecord);
-
- static void HandleEvent (
- EventRecord *theEvent);
-
- static void HandleActivate (
- WindowPtr pWin,
- Boolean becomingActive);
-
- static Boolean IsAppWindow (
- WindowPtr pWin);
-
- static pascal Boolean HandleAlertEventFilter (
- DialogPtr pDlog,
- EventRecord *pEvent,
- SInt16 *itemHit);
-
- static OSErr SetupFWXNode (void);
-
- static OSErr InitNotification (void);
-
- pascal void HandleNotifyResponse (
- NMRecPtr pNMRequest);
-
-
- static OSErr SetupIOQueue (void);
-
- static OSErr FWXInit (void);
-
- static OSErr FWXDispose (void);
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // External procedure prototypes.
- //
-
- extern OSErr InstallDragHandlers (void);
-
- extern void RemoveDragHandlers (void);
-
- extern pascal OSErr HandleAEFileSpecList (
- AppleEvent *pAEvent,
- AppleEvent *pReply,
- SInt32 refCon);
-
- extern OSErr GetNodeDragRect (
- WindowPtr pWin,
- SInt16 spaceIndex,
- Rect *iconRect,
- Rect *textRect);
-
- extern void HandleReceive (void);
-
- extern void HandleForkReadComplete (void);
-
- extern OSErr FSGetCatInfo (
- FSSpec *fsSpec,
- CInfoPBPtr result);
-
- extern void UpdateProgressBar (
- WindowPtr theWindow,
- SInt16 itemNo);
-
- extern OSErr GetNodeName (
- WindowPtr pWin,
- SInt16 spaceIndex,
- StringPtr *pString);
-
- extern void CleanupCopyDialog(
- WindowPtr theDialog);
-
- extern OSErr GetNodeInfo (
- FWXNodeID nodeID,
- RecvNodePtr *pRecvNode);
-
- extern OSErr HandleStopTransfer(
- CurFileInfoPtr pCurFileInfo);
-
- extern OSErr GetCurFileInfo(
- FWXNodeID sourceID,
- CurFileInfoPtr *hCurFileInfo,
- Boolean createNew);
-
- extern OSErr InitRecvFolder(
- SInt32 *dirID);
-
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // Globals JKL *** these all need to be cleaned up
- //
- #ifndef __MWERKS__
- QDGlobals qd;
- #endif
-
- FWXAppDataPtr gpFWXAppData = nil;
-
- Boolean queuesInitialized = false;
- QHdr gSendQHdr;
- QHdr gAESendQHdr;
- QHdr gReceiveQHdr;
- QHdr gFileReadQHdr;
- QHdr gFWControlQHdr;
- QHdr gFWWriteQHdr;
- QHdr gCurFileQHdr;
- SInt16 gCurForkRefNum;
-
- Boolean gSendingFile,
- gCheckingTransfer,
- gSendingDataFork,
- gSendingResFork,
- gForkWriteComplete,
- gForkComplete;
-
- FWXNodeID gSendError;
- FWXNodeID gReceiveError;
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // SendDeviceAddedToSelf
- //
- // Send a firewire device added appleevent to us
- //
- static OSErr SendDeviceAddedToSelf(
- FWXNodeID theNodeID)
- {
- AppleEvent send;
- AppleEvent reply;
- AEDesc selfTarget;
- ProcessSerialNumber psn;
- OSErr err;
-
- selfTarget.dataHandle = nil;
- send.dataHandle = nil;
-
- // Get our address
- GetCurrentProcess(&psn);
- err = AECreateDesc(typeProcessSerialNumber, (Ptr) &psn, sizeof(ProcessSerialNumber), &selfTarget);
- if (err != noErr)
- return err;
-
- err = AECreateAppleEvent(kAEFWXEventClass, kAEFWXDeviceAdded, &selfTarget,
- kAutoGenerateReturnID, kAnyTransactionID, &send);
- if (err != noErr) {
- AEDisposeDesc(&selfTarget);
- return err;
- }
-
- err = AEPutParamPtr(&send, kAEFWXNodeIDKey, kAEFWXNodeIDType, (Ptr) &theNodeID, sizeof(FWXNodeID));
-
- err = AESend(&send, &reply, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer,
- kAENormalPriority, kAEDefaultTimeout, nil, nil);
-
- if (err != noErr) {
- AEDisposeDesc(&selfTarget);
- AEDisposeDesc(&send);
- }
-
- return err;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // SendDeviceRemovedToSelf
- //
- // Send a firewire device removed appleevent to us
- //
- static OSErr SendDeviceRemovedToSelf(
- FWXNodeID theNodeID)
- {
- AppleEvent send;
- AppleEvent reply;
- AEDesc selfTarget;
- ProcessSerialNumber psn;
- OSErr err;
-
- selfTarget.dataHandle = nil;
- send.dataHandle = nil;
-
- // Get our address
- GetCurrentProcess(&psn);
- err = AECreateDesc(typeProcessSerialNumber, (Ptr) &psn, sizeof(ProcessSerialNumber), &selfTarget);
- if (err != noErr)
- return err;
-
- err = AECreateAppleEvent(kAEFWXEventClass, kAEFWXDeviceRemoved, &selfTarget,
- kAutoGenerateReturnID, kAnyTransactionID, &send);
- if (err != noErr) {
- AEDisposeDesc(&selfTarget);
- return err;
- }
-
- err = AEPutParamPtr(&send, kAEFWXNodeIDKey, kAEFWXNodeIDType, (Ptr) &theNodeID, sizeof(FWXNodeID));
-
- err = AESend(&send, &reply, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer,
- kAENormalPriority, kAEDefaultTimeout, nil, nil);
-
- if (err != noErr) {
- AEDisposeDesc(&selfTarget);
- AEDisposeDesc(&send);
- }
-
- return err;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleDeviceAddedEvent
- //
- // Called when a new FireWire File Exchange receiver connects to the bus.
- // Add it to the list of open nodes. Prime it with receive buffers. Send our
- // machine info to it.
- //
- static pascal OSErr HandleDeviceAddedEvent (
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long handlerRefcon)
- {
- WindowPtr pWin;
- WindowDataPtr pWinData;
- OpenNodePtr pOpenNode;
- OpenNodePtr pTempOpenNode;
- FWXNodeID nodeID;
- OSErr err = noErr;
-
- // get node id out of appleevent
- err = GetNodeIDFromEvent(theAppleEvent, &nodeID);
-
- if (err == noErr)
- pOpenNode = GetNodeInfoFromID(nodeID);
-
- if ((err == noErr) && (pOpenNode == nil))
- {
- pWin = gpFWXAppData->pSenderWindow;
- pWinData = (WindowDataPtr) GetWRefCon(pWin);
- if (pWinData == nil)
- err = memFullErr;
-
- if (err == noErr)
- {
- // create open node record.
- pOpenNode = (OpenNodePtr) NewPtrClear(sizeof(OpenNodeRecord));
- if (pOpenNode != nil)
- pOpenNode->nodeID = nodeID;
- else
- err = memFullErr;
- }
-
- if (err == noErr)
- // open the node
- err = OpenFWXNode(nodeID);
-
- if (err == noErr)
- {
- pWinData->numOpenNodes++;
-
- // add the node to the open list
- // if this is the first node, just point to it
- if (pWinData->numOpenNodes == 1)
- pWinData->pOpenNodeList = pOpenNode;
- else
- {
- // it is not the first node, traverse the list, add new node to the end
- pTempOpenNode = pWinData->pOpenNodeList;
- while (pTempOpenNode->pNextNode != nil)
- pTempOpenNode = pTempOpenNode->pNextNode;
- pTempOpenNode->pNextNode = pOpenNode;
- pOpenNode->pPreviousNode = pTempOpenNode;
- }
- }
-
- if (err != noErr)
- {
- // clean up on error
- if (pOpenNode != nil)
- DisposePtr((Ptr) pOpenNode);
- }
-
- if (err == noErr)
- // issue reads to prime node with receive buffers
- err = SetupFWXRead(nodeID);
-
- if (err == noErr)
- // send the new node our name and id info
- err = SendFWXInfo(nodeID, kNodeInfoRequest);
-
- if (err == memFullErr)
- HandleStopAlert(kMemoryErrorAlertID);
- else if (err != noErr)
- // JKL *** need a generic error alert mechanism
- HandleStopAlert(kUnknownErrAlertID);
- }
- return err;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // GetNodeInfoFromID
- //
- // Search open node list for node record corresponding to node id.
- //
- static OpenNodePtr GetNodeInfoFromID (
- FWXNodeID nodeID)
- {
- WindowDataPtr pWinData;
- OpenNodePtr pOpenNode;
- Boolean found;
-
- pWinData = (WindowDataPtr) GetWRefCon(gpFWXAppData->pSenderWindow);
- pOpenNode = pWinData->pOpenNodeList;
-
- found = false;
- while ((pOpenNode != nil) && (!found))
- {
- if (pOpenNode->nodeID == nodeID)
- found = true;
- else
- pOpenNode = pOpenNode->pNextNode;
- }
- return pOpenNode;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // SendFWXInfo
- //
- // Send our info to another node. Get the sharing setup machine name out
- // of string resource.
- //
- OSErr SendFWXInfo(
- FWXNodeID nodeID,
- UInt32 sendType)
- {
- Handle hString;
- IOParamPtr pIOPb;
- FWXPacketPtr pPktInfo;
- SInt16 curResFile;
- OSErr err = noErr;
-
- // Get the sharing setup macintosh name
- curResFile = CurResFile();
- UseResFile(kSystemResFile);
- hString = Get1Resource('STR ', kNetworkNameID);
-
- if ((hString != nil) && (**hString != 0))
- {
- FWIXDequeue ((QElemPtr *) &pIOPb, &gFWControlQHdr);
- if (pIOPb != nil)
- {
- SetupFWControlPB(pIOPb);
- // SetupFWControlPB will leave an invalid node id in nameptr at this point
- pIOPb->ioNamePtr = (StringPtr) nodeID;
-
- // copy the string into the data buffer
- pPktInfo = (FWXPacketPtr) pIOPb->ioBuffer;
- pPktInfo->packetType = sendType;
- pIOPb->ioMisc = (Ptr) sendType;
- BlockMove(*hString, (Ptr) &pPktInfo->packetData, **hString + 1);
- pIOPb->ioReqCount = **hString + 1 + 4; // length of string plus packet header
-
- err = CallFWXNode(pIOPb);
- }
- else
- {
- err = qErr;
- FWDebugStr("\pNo buffer for SendFWXInfo");
- }
-
- ReleaseResource(hString);
- }
- else
- {
- HandleStopAlert(kNoNameAlertID);
- ExitToShell();
- }
- UseResFile(curResFile);
- return err;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // SendFWXQuit
- //
- // Send a quit notification to each receive node.
- //
- OSErr SendFWXQuit(void)
- {
- WindowDataPtr pWinData;
- OpenNodePtr pOpenNode;
- IOParamPtr pIOPb;
- FWXPacketPtr pPktInfo;
- SInt16 i;
- OSErr err = noErr;
-
- pWinData = (WindowDataPtr) GetWRefCon(gpFWXAppData->pSenderWindow);
- if (pWinData != nil)
- {
- pOpenNode = pWinData->pOpenNodeList;
- for (i = 0; i < pWinData->numOpenNodes; i++)
- {
- FWIXDequeue ((QElemPtr *) &pIOPb, &gFWControlQHdr);
- if (pIOPb != nil)
- {
- SetupFWControlPB(pIOPb);
- // SetupFWWritePB will leave an invalid driver id in nameptr at this point
- pIOPb->ioNamePtr = (StringPtr) pOpenNode->nodeID;
-
- // copy the string into the data buffer
- pPktInfo = (FWXPacketPtr) pIOPb->ioBuffer;
- pPktInfo->packetType = kQuitNotify;
- pIOPb->ioMisc = (Ptr) kQuitNotify;
- pIOPb->ioReqCount = 4;
-
- err = CallFWXNode(pIOPb);
- }
- else
- {
- err = qErr;
- FWDebugStr("\pNo buffer for SendFWXQuit");
- }
- pOpenNode = (OpenNodePtr) pOpenNode->pNextNode;
- }
- }
- else
- err = resNotFound;
-
- return err;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // SetupFWXRead
- //
- // Create control buffers and a data buffer and pass the buffers to
- // the driver.
- //
- static OSErr SetupFWXRead(
- FWXNodeID nodeID)
- {
- IOParamPtr pIOPb;
- SInt16 i;
- OSErr err = noErr;
-
- // create control receive parameter block, receive buffer, and issue read
- for (i=0; i < kFWReadControlBufs; i++) {
- pIOPb = (IOParamPtr) NewPtrClear(sizeof(IOParam));
- if (pIOPb != nil) {
- HoldMemory ((Ptr) pIOPb, sizeof(IOParam));
- } else {
- FWDebugStr("\pOut of memory, SetupReadQueue");
- err = memFullErr;
- break;
- }
-
- pIOPb->ioBuffer = NewPtr(kFWReadControlBufSize);
- if (pIOPb->ioBuffer != nil) {
- HoldMemory (pIOPb->ioBuffer, kFWReadControlBufSize);
- } else {
- UnholdMemory ((Ptr) pIOPb, sizeof(IOParam));
- DisposePtr((Ptr) pIOPb);
- FWDebugStr("\pOut of memory, SetupReadQueue");
- err = memFullErr;
- break;
- }
-
- // set up parameter block
- pIOPb->ioNamePtr = (StringPtr) nodeID;
- pIOPb->ioTrap = kFWXRead;
- pIOPb->ioCompletion = (IOCompletionUPP) HandleFWReadComplete;
- pIOPb->ioReqCount = kFWReadControlBufSize;
-
- err = CallFWXNode(pIOPb);
- if (err != noErr) {
- sprintf(debugStr, "Error in CallFWXNode, SetupReadQueue: %d", err);
- FWDebugStr((ConstStr255Param) c2pstr(debugStr));
- break;
- }
- }
-
- if (err == noErr) {
- // setup the data read parameter blocks
- for (i=0; i < kFWReadDataBufs; i++) {
- pIOPb = (IOParamPtr) NewPtrClear(sizeof(IOParam));
- if (pIOPb != nil) {
- HoldMemory ((Ptr) pIOPb, sizeof(IOParam));
- } else {
- FWDebugStr("\pOut of memory, SetupReadQueue");
- err = memFullErr;
- break;
- }
-
- pIOPb->ioBuffer = NewPtr(kFWReadDataBufSize);
- if (pIOPb->ioBuffer != nil) {
- HoldMemory (pIOPb->ioBuffer, kFWReadDataBufSize);
- } else {
- UnholdMemory ((Ptr) pIOPb, sizeof(IOParam));
- DisposePtr((Ptr) pIOPb);
- FWDebugStr("\pOut of memory, SetupReadQueue");
- err = memFullErr;
- break;
- }
-
- // set up parameter block
- pIOPb->ioNamePtr = (StringPtr) nodeID;
- pIOPb->ioTrap = kFWXRead;
- pIOPb->ioCompletion = (IOCompletionUPP) HandleFWReadComplete;
- pIOPb->ioReqCount = kFWReadDataBufSize;
- pIOPb->ioMisc = (Ptr) kForkData;
-
- err = CallFWXNode(pIOPb);
- if (err != noErr) {
- sprintf(debugStr, "Error in CallFWXNode, SetupReadQueue: %d", err);
- FWDebugStr((ConstStr255Param) c2pstr(debugStr));
- break;
- }
- }
- }
- return err;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // GetNodeIDFromEvent
- //
- // Get the firewire file exchange node ID
- // out of the device added or removed apple event
- //
- static OSErr GetNodeIDFromEvent(
- AppleEvent *pAEvent,
- FWXNodeID *nodeID)
- {
- OSErr err;
- DescType returnType;
- Size returnSize;
-
- err = AEGetParamPtr(pAEvent, kAEFWXNodeIDKey, kAEFWXNodeIDType, &returnType,
- (Ptr) nodeID, sizeof(FWXNodeID), &returnSize);
- return err;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // NewSenderWindow
- //
- // Creates firwire sender window. Called when the first
- // FireWire Exchange receive node is connected to the bus.
- //
-
- static OSErr NewSenderWindow (void)
- {
- WindowRef pWin;
- GrafPtr curPort;
- WindowDataPtr pWinData;
- MenuHandle hMenu;
- Rect winRect;
- OSErr err = noErr;
-
- // open the window
- pWin = GetNewCWindow(kWindowResourceID, nil, (WindowRef) -1);
-
- if (pWin == nil)
- err = ResError();
- else
- {
- // position and size window
- err = GetWindowPos(&winRect);
- if (err == noErr)
- {
- MoveWindow(pWin, winRect.left, winRect.top, false);
- SizeWindow(pWin, winRect.right - winRect.left, winRect.bottom - winRect.top, false);
- }
- else
- err = noErr; // don't care if resource problem
-
- // Allocate window data structure
- pWinData = (WindowDataPtr) NewPtrClear(sizeof(WindowData));
- if (pWinData != nil)
- {
- pWinData->numOpenNodes = 0;
- pWinData->pOpenNodeList = nil;
- pWinData->numRecvNodes = 0;
- pWinData->pRecvNodeList = nil;
- pWinData->hVScrollBar = GetNewControl(kVScrollBar, pWin);
- if (pWinData->hVScrollBar == nil)
- err = ResError();
- pWinData->hHScrollBar = GetNewControl(kHScrollBar, pWin);
- if (pWinData->hHScrollBar == nil)
- err = ResError();
-
- SetWRefCon(pWin, (SInt32) pWinData);
- if (err == noErr)
- AdjustScrollBars(pWin, true);
- }
- else
- {
- err = memFullErr;
- DisposeWindow(pWin);
- }
- }
- gpFWXAppData->pSenderWindow = pWin;
- if (err == noErr)
- {
- GetPort(&curPort);
- SetPort(pWin);
- TextSize(9);
- ClipRect(&qd.screenBits.bounds);
- SetPort(curPort);
- }
- if (err == noErr)
- {
- if (gpFWXAppData->fwixPrefs & kIconView)
- {
- pWinData->windowView = kSmallIconView;
- hMenu = GetMenuHandle(kViewMenuID);
- SetItemMark(hMenu, kSmallIconMenuItem, checkMark);
- SetItemMark(hMenu, kIconMenuItem, noMark);
- }
- else
- pWinData->windowView = kLargeIconView;
- }
-
- return err;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // GetDisplayRect
- //
- // Calculate the rectangle needed to display all of the sender window items.
- //
- static void GetDisplayRect (
- Rect *r,
- WindowPtr pWin,
- WindowDataPtr pWinData)
- {
- GrafPtr curPort;
- Rect iconRect, textRect;
- SInt16 row, column;
- SInt16 index, count;
-
- GetPort(&curPort);
- SetPort(pWin);
- TextSize(9);
-
- if (pWinData->windowView == kLargeIconView)
- {
- row = pWinData->numRecvNodes % kLargeRowCount;
- column = pWinData->numRecvNodes / kLargeRowCount;
- if (column == 0)
- count = row;
- else
- count = kLargeRowCount;
-
- // get smallest left value
- for (index = 1; index <= count; index++)
- {
- GetNodeDragRect(pWin, index, &iconRect, &textRect);
- if (textRect.left < iconRect.left)
- {
- if (textRect.left < r->left)
- r->left = textRect.left;
- }
- else
- {
- if (iconRect.left < r->left)
- r->left = iconRect.left;
- }
- }
-
- // get smallest top value
- GetNodeDragRect(pWin, 1, &iconRect, &textRect);
- if (iconRect.top < r->top)
- r->top = iconRect.top;
-
- // get largest bottom value from lowest icon in row
- GetNodeDragRect(pWin, count, &iconRect, &textRect);
- if (textRect.bottom > r->bottom)
- r->bottom = textRect.bottom;
-
- // get largest right value by checking space needed for far right column
- for (index = ((column * kLargeRowCount) + 1); index <= pWinData->numRecvNodes; index++)
- {
- GetNodeDragRect(pWin, index, &iconRect, &textRect);
- if (textRect.right > iconRect.right)
- {
- if (textRect.right > r->right)
- r->right = textRect.right;
- }
- else
- {
- if (iconRect.right > r->right)
- r->right = iconRect.right;
- }
- }
- }
- else
- {
- row = pWinData->numRecvNodes % kSmallRowCount;
- column = pWinData->numRecvNodes / kSmallRowCount;
- if (column == 0)
- count = row;
- else
- count = kSmallRowCount;
-
- // get smallest top left value
- GetNodeDragRect(pWin, 1, &iconRect, &textRect);
- if (iconRect.top < r->top)
- r->top = iconRect.top;
- if (iconRect.left < r->left)
- r->left = iconRect.left;
-
- // get largest bottom value from lowest icon in row
- GetNodeDragRect(pWin, count, &iconRect, &textRect);
- if (iconRect.bottom > r->bottom)
- r->bottom = iconRect.bottom;
-
- // get largest right value by checking space needed for far right column
- for (index = ((column * kSmallRowCount) + 1); index <= pWinData->numRecvNodes; index++)
- {
- GetNodeDragRect(pWin, index, &iconRect, &textRect);
- if (textRect.right > r->right)
- r->right = textRect.right;
- }
- }
- SetPort(curPort);
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // AdjustScrollBars
- //
- // Adjust and size scroll bars
- //
- OSErr AdjustScrollBars (
- WindowPtr pWin,
- Boolean adjustSizes)
- {
- WindowDataPtr pWinData;
- Rect viewRect, // view able area in window
- displayRect; // area needed to display node list
- Rect ctlRect;
- SInt16 displayH, displayV, viewH, viewV;
- SInt16 ctlVal;
- OSErr err = noErr;
-
- pWinData = (WindowDataPtr) GetWRefCon(pWin);
- if (pWinData != nil)
- {
- // calculate view space available
- viewRect = pWin->portRect;
- viewRect.bottom -= kScrollBarAdjust;
- viewRect.right -= kScrollBarAdjust;
- displayRect = viewRect;
-
- GetDisplayRect (&displayRect, pWin, pWinData);
- displayH = displayRect.right - displayRect.left;
- viewH = viewRect.right - viewRect.left;
-
- if (displayH > viewH)
- {
- SetControlMaximum (pWinData->hHScrollBar, displayH - viewH);
- ctlVal = GetControlValue(pWinData->hHScrollBar);
- SetControlValue (pWinData->hHScrollBar, ctlVal + (viewRect.left - displayRect.left));
- }
- else
- {
- SetControlMaximum (pWinData->hHScrollBar, 0);
- SetControlValue (pWinData->hHScrollBar, 0);
- }
-
- displayV = displayRect.bottom - displayRect.top;
- viewV = viewRect.bottom - viewRect.top;
- if (displayV > viewV)
- {
- SetControlMaximum (pWinData->hVScrollBar, displayV - viewV);
- ctlVal = GetControlValue(pWinData->hVScrollBar);
- SetControlValue (pWinData->hVScrollBar, ctlVal + (viewRect.top - displayRect.top));
- }
- else
- {
- SetControlMaximum (pWinData->hVScrollBar, 0);
- SetControlValue (pWinData->hVScrollBar, 0);
- }
-
- if (adjustSizes)
- {
- // position and size vertical scroll bar
- ctlRect = pWin->portRect;
- MoveControl (pWinData->hVScrollBar,
- ctlRect.right - kScrollBarAdjust,
- -1);
- SizeControl (pWinData->hVScrollBar,
- kScrollBarWidth,
- (ctlRect .bottom - ctlRect.top) - (kScrollBarAdjust - kScrollBarTweek));
-
- // position and size horizontal scroll bar
- MoveControl (pWinData->hHScrollBar,
- -1,
- ctlRect.bottom - kScrollBarAdjust);
- SizeControl (pWinData->hHScrollBar,
- (ctlRect.right - ctlRect.left) - (kScrollBarAdjust - kScrollBarTweek),
- kScrollBarWidth);
- }
- }
- else
- err = resNotFound;
- return err;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // InitRecvNode
- //
- // Set up the node drag rectangles and node list.
- //
- OSErr InitRecvNode (
- FWXNodeID nodeID,
- ConstStr255Param nodeName)
- {
- WindowPtr pWin;
- WindowDataPtr pWinData;
- RecvNodePtr pRecvNode;
- RecvNodePtr pTempRecvNode;
- SInt16 rctTop, rctLeft, rctBottom, rctRight;
- SInt16 row, column;
- SInt16 sLoc, sWidth;
- OSErr err = noErr;
-
- pWin = gpFWXAppData->pSenderWindow;
- pWinData = (WindowDataPtr) GetWRefCon(pWin);
- if (pWinData == nil)
- return memFullErr;
-
- pRecvNode = (RecvNodePtr) NewPtrClear(sizeof(RecvNodeRecord));
- if (pRecvNode == nil)
- return memFullErr;
-
- pRecvNode->pReplyTimer = (MyTMTaskPtr) NewPtrClear(sizeof(MyTMTask));
- if (pRecvNode->pReplyTimer == nil)
- return memFullErr;
- else
- {
- pRecvNode->pReplyTimer->timerTask.tmAddr = NewTimerProc(HandleReplyTimeout);
- if (pRecvNode->pReplyTimer->timerTask.tmAddr == nil)
- return memFullErr;
-
- InsTime((QElemPtr) pRecvNode->pReplyTimer);
- }
-
- // init values
- pRecvNode->nodeID = nodeID;
- BlockMove(nodeName, pRecvNode->nodeName, nodeName[0] + 1);
- sWidth = StringWidth(pRecvNode->nodeName);
-
- // setup node drag rectangle
- if (pWinData->windowView == kLargeIconView)
- {
- row = pWinData->numRecvNodes % kLargeRowCount;
- column = pWinData->numRecvNodes / kLargeRowCount;
- rctLeft = kLargeHFill + (column * kLargeHSize);
- rctRight = rctLeft + kLargeIconFill;
- rctTop = kLargeVFill + (row * kLargeVSize);
- rctBottom = rctTop + kLargeIconFill;
-
- sLoc = rctLeft + ((rctRight - rctLeft) / 2);
- sLoc -= ((sWidth / 2) + 2);
- SetRect(&pRecvNode->recvNodeTextRect,
- sLoc,
- rctBottom,
- sLoc + sWidth + 3,
- rctBottom + 13);
- }
- else
- {
- row = pWinData->numRecvNodes % kSmallRowCount;
- column = pWinData->numRecvNodes / kSmallRowCount;
- rctLeft = kSmallHFill + (column * kSmallHSize);
- rctRight = rctLeft + kSmallIconFill;
- rctTop = kSmallVFill + (row * kSmallVSize);
- rctBottom = rctTop + kSmallIconFill;
-
- SetRect(&pRecvNode->recvNodeTextRect,
- rctRight + 2,
- rctBottom - 15,
- rctRight + sWidth + 5,
- rctBottom - 2);
- }
- SetRect(&pRecvNode->recvNodeIconRect, rctLeft, rctTop, rctRight, rctBottom);
-
- // increment node count after updating rectangles
- pWinData->numRecvNodes++;
-
- // add the node to the list, if the the first node, just point to it
- if (pWinData->numRecvNodes == 1)
- pWinData->pRecvNodeList = pRecvNode;
- else
- {
- // it is not the first node, traverse the list, add new node to the end
- pTempRecvNode = pWinData->pRecvNodeList;
- while (pTempRecvNode->pNextNode != nil)
- pTempRecvNode = pTempRecvNode->pNextNode;
- pTempRecvNode->pNextNode = pRecvNode;
- pRecvNode->pPreviousNode = pTempRecvNode;
- }
-
- AdjustScrollBars(pWin, false);
-
- return err;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleReplyTimeout
- //
- // For various communications with nodes, fwix posts a timer to wait for
- // a reply. If no reply is received this task is executed.
- //
- // JKL *** needs to have a variable timer to increase timeout period from
- // a few seconds up to a couple of minutes and eventually give up
- //
- static pascal void HandleReplyTimeout (
- MyTMTaskPtr pTMTask)
- {
- gSendError = pTMTask->nodeID;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleDeviceRemovedEvent
- //
- // Called when a FWIX receiver node disconnects from the bus. If the node
- // had fwix open, clean up the node info and any sends. Otherwise close the node.
- //
- // JKL *** what if we are receiving data from this node? handle by HandleReceive timeout?
- static pascal OSErr HandleDeviceRemovedEvent (
- AppleEvent *theAppleEvent,
- AppleEvent *reply,
- long handlerRefcon)
- {
- WindowDataPtr pWinData;
- GrafPtr curPort;
- GrafPtr pWindowPort;
- RecvNodePtr pTempRecvNode;
- OpenNodePtr pTempOpenNode;
- FWXNodeID targetNodeID;
- TxFSSpecPtr pTxItem;
- OSErr err;
- Boolean foundNodeID;
-
- err = GetNodeIDFromEvent(theAppleEvent, &targetNodeID);
- if ((err == noErr) && (gSendingFile || gCheckingTransfer))
- {
- // if we are sending a file to the removed node,
- // dismiss the dialog, clean up send queue, and alert user
- if (targetNodeID == ((TxFSSpecPtr) gSendQHdr.qHead)->recvNode)
- {
- gCheckingTransfer = false;
- gSendingFile = false;
- gForkComplete = true;
- gForkWriteComplete = true;
- CleanupCopyDialog(FrontWindow());
- if (gSendingDataFork || gSendingResFork)
- {
- gSendingDataFork = false;
- gSendingResFork = false;
- FSClose(gCurForkRefNum);
- }
-
- // dequeue and dispose of all file queue entries
- pTxItem = (TxFSSpecPtr) gSendQHdr.qHead;
- while (pTxItem != nil)
- {
- Dequeue((QElemPtr) pTxItem, &gSendQHdr);
- DisposePtr((Ptr) pTxItem->pFSSpec);
- DisposePtr((Ptr) pTxItem);
-
- // get another queue item
- pTxItem = (TxFSSpecPtr) gSendQHdr.qHead;
- }
-
- GetNodeInfo(targetNodeID, &pTempRecvNode);
- ParamText(pTempRecvNode->nodeName, "\p", "\p", "\p");
- HandleCautionAlert(kNodeDisconnectAlertID);
- }
- }
-
- // Get the window data record
- pWinData = (WindowDataPtr) GetWRefCon(gpFWXAppData->pSenderWindow);
-
- // Traverse the open node list to find the node record for the nodeID
- foundNodeID = false;
- pTempOpenNode = pWinData->pOpenNodeList;
- while ((pTempOpenNode != nil) && (!foundNodeID))
- {
- if (pTempOpenNode->nodeID == targetNodeID)
- {
- foundNodeID = true;
- break;
- }
- pTempOpenNode = pTempOpenNode->pNextNode;
- }
-
- if (foundNodeID)
- {
- // handle removing the node from the open node list
- if (pTempOpenNode->pNextNode != nil)
- pTempOpenNode->pNextNode->pPreviousNode = pTempOpenNode->pPreviousNode;
-
- if (pTempOpenNode->pPreviousNode != nil)
- pTempOpenNode->pPreviousNode->pNextNode = pTempOpenNode->pNextNode;
- else
- pWinData->pOpenNodeList = pTempOpenNode->pNextNode;
-
- DisposePtr((Ptr) pTempOpenNode);
- pWinData->numOpenNodes--;
- CloseFWXNode(targetNodeID);
- }
-
- // Traverse the receive node list to find the receive node record for the nodeID
- foundNodeID = false;
- pTempRecvNode = pWinData->pRecvNodeList;
- while ((pTempRecvNode != nil) && (!foundNodeID))
- {
- if (pTempRecvNode->nodeID == targetNodeID)
- {
- foundNodeID = true;
- break;
- }
- pTempRecvNode = pTempRecvNode->pNextNode;
- }
-
- if (foundNodeID)
- {
- // remove the node from the list
- if (pTempRecvNode->pNextNode != nil)
- pTempRecvNode->pNextNode->pPreviousNode = pTempRecvNode->pPreviousNode;
-
- if (pTempRecvNode->pPreviousNode != nil)
- pTempRecvNode->pPreviousNode->pNextNode = pTempRecvNode->pNextNode;
- else
- pWinData->pRecvNodeList = pTempRecvNode->pNextNode;
-
- // JKL *** how about canceling timer?
- if (pTempRecvNode->pReplyTimer != nil)
- {
- if (pTempRecvNode->pReplyTimer->timerTask.tmAddr != nil)
- DisposeRoutineDescriptor(pTempRecvNode->pReplyTimer->timerTask.tmAddr);
- DisposePtr((Ptr) pTempRecvNode->pReplyTimer);
- }
- DisposePtr((Ptr) pTempRecvNode);
- pWinData->numRecvNodes--;
-
- if (pWinData->numRecvNodes == 0)
- {
- HideWindow(gpFWXAppData->pSenderWindow);
- DisableItem(GetMenuHandle(kFileMenuID), kOpenMenuItem);
- DisableItem(GetMenuHandle(kFileMenuID), kCloseMenuItem);
- DisableItem(GetMenuHandle(kViewMenuID), 0);
- DrawMenuBar();
- }
- else
- {
- // Force an update
- GetPort(&curPort);
- pWindowPort = (GrafPtr) GetWindowPort(gpFWXAppData->pSenderWindow);
- AdjustNodeIcons(pWinData, &pWindowPort->portRect);
- AdjustScrollBars(gpFWXAppData->pSenderWindow, false);
- SetPortWindowPort(pWindowPort);
- InvalRect(&pWindowPort->portRect);
- SetPort(curPort);
- }
- }
- return err;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // AdjustNodeIcons
- //
- // Adjust the placement of the node icons in the window. Traverse the list
- // of nodes setting the rectangle appropriately.
- //
-
- void AdjustNodeIcons (
- WindowDataPtr pWinData,
- Rect *windRect)
- {
- RecvNodePtr pRecvNode;
- SInt16 rctTop, rctLeft, rctBottom, rctRight;
- SInt16 row, column;
- SInt16 index;
- SInt16 sWidth, sLoc;
-
- pRecvNode = pWinData->pRecvNodeList;
- for (index = 0; index < pWinData->numRecvNodes; index++)
- {
- if (pRecvNode == nil)
- FWDebugStr("\pNode List screwed up in AdjustNodeIcons");
-
- // setup drag rectangle
- sWidth = StringWidth(pRecvNode->nodeName);
- if (pWinData->windowView == kLargeIconView)
- {
- row = index % kLargeRowCount;
- column = index / kLargeRowCount;
- rctLeft = kLargeHFill + (column * kLargeHSize);
- rctRight = rctLeft + kLargeIconFill;
- rctTop = kLargeVFill + (row * kLargeVSize);
- rctBottom = rctTop + kLargeIconFill;
-
- sLoc = rctLeft + ((rctRight - rctLeft) / 2);
- sLoc -= ((sWidth / 2) + 2);
- SetRect(&pRecvNode->recvNodeTextRect,
- sLoc,
- rctBottom,
- sLoc + sWidth + 3,
- rctBottom + 13);
- }
- else
- {
- row = index % kSmallRowCount;
- column = index / kSmallRowCount;
- rctLeft = kSmallHFill + (column * kSmallHSize);
- rctRight = rctLeft + kSmallIconFill;
- rctTop = kSmallVFill + (row * kSmallVSize);
- rctBottom = rctTop + kSmallIconFill;
-
- SetRect(&pRecvNode->recvNodeTextRect,
- rctRight + 2,
- rctBottom - 15,
- rctRight + sWidth + 5,
- rctBottom - 2);
- }
- SetRect(&pRecvNode->recvNodeIconRect, rctLeft, rctTop, rctRight, rctBottom);
- pRecvNode = (RecvNodePtr) pRecvNode->pNextNode;
- }
- }
-
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleGetNodeListEvent
- //
- // Handle the get node list applescript command
- //
-
- static pascal OSErr HandleGetNodeListEvent (
- AppleEvent *pAppleEvent,
- AppleEvent *pReplyEvent,
- SInt32 refCon)
- {
- WindowDataPtr pWinData;
- RecvNodePtr pNodeInfo;
- AEDescList nodeList;
- OSErr err = noErr;
-
- err = AECreateList(nil, 0, false, &nodeList);
- if (err == noErr) {
-
- pWinData = (WindowDataPtr) GetWRefCon(gpFWXAppData->pSenderWindow);
- if (pWinData != nil)
- {
- pNodeInfo = pWinData->pRecvNodeList;
- while ((pNodeInfo != nil) && (!err))
- {
- err = AEPutPtr(&nodeList, 0, typeChar, pNodeInfo->nodeName + 1, pNodeInfo->nodeName[0]);
- pNodeInfo = pNodeInfo->pNextNode;
- }
- if (err == noErr)
- err = AEPutKeyDesc(pReplyEvent, keyDirectObject, &nodeList);
- }
- }
-
- return err;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleSendItemsEvent
- //
- // Handle the send applescript command
- //
- static pascal OSErr HandleSendItemsEvent (
- AppleEvent *pAppleEvent,
- AppleEvent *pReplyEvent,
- SInt32 refCon)
- {
- FWXNodeID *pNodeIDList = nil;
- FSSpecPtr pFileSpecList = nil;
- AESendRecPtr pAESendElem;
- UInt16 numFSItems;
- UInt16 numNodes;
- UInt16 index;
- OSErr err;
-
- // get items to be sent from event
- err = GetFSItemsFromEvent(&pFileSpecList, pAppleEvent, &numFSItems);
-
- // get the list of receive targets from event
- if (err == noErr)
- err = GetReceiveNodesFromEvent(&pNodeIDList, pAppleEvent, &numNodes);
-
- if (err == noErr)
- {
- for (index = 0; index < numNodes; index++)
- {
- pAESendElem = (AESendRecPtr) NewPtr(sizeof(AESendRec));
- if (pAESendElem == nil)
- {
- err = memFullErr;
- break;
- }
- else
- {
- pAESendElem->pSendItemsList = (FSSpecPtr) NewPtr(sizeof(FSSpec) * numFSItems);
- if (pAESendElem->pSendItemsList != nil)
- {
- BlockMove(pFileSpecList, pAESendElem->pSendItemsList, sizeof(FSSpec) * numFSItems);
- pAESendElem->recvNode = pNodeIDList[index];
- pAESendElem->numSendItems = numFSItems;
- Enqueue((QElemPtr) pAESendElem, &gAESendQHdr);
- }
- else
- {
- err = memFullErr;
- break;
- }
- }
- }
- }
-
- if (pNodeIDList != nil)
- DisposePtr((Ptr) pNodeIDList);
- if (pFileSpecList != nil)
- DisposePtr((Ptr) pFileSpecList);
-
- if (!(gSendingFile || gCheckingTransfer))
- {
- pAESendElem = (AESendRecPtr) gAESendQHdr.qHead;
- if (pAESendElem != nil)
- {
- Dequeue((QElemPtr) pAESendElem, &gAESendQHdr);
- SendFSSpecListToSelf(pAESendElem->recvNode, pAESendElem->pSendItemsList, pAESendElem->numSendItems);
- DisposePtr((Ptr) pAESendElem->pSendItemsList);
- DisposePtr((Ptr) pAESendElem);
- }
- }
-
- err = AEPutKeyPtr(pReplyEvent, keyDirectObject, typeShortInteger, &err, sizeof(OSErr));
- return err;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // GetReceiveNodesFromEvent
- //
- // Get the target nodes for the transfer out of the event
- //
- static OSErr GetReceiveNodesFromEvent (
- FWXNodeID **pReceiveNodeIDList,
- AppleEvent *pAppleEvent,
- UInt16 *numNodes)
- {
- Ptr p = nil;
- AEDescList descriptorList;
- SInt32 itemCount;
- AEKeyword keywd;
- DescType returnedType;
- Size actualSize;
- UInt16 i;
- OSErr err;
-
- err = AEGetParamDesc(pAppleEvent, kAEFWXNodeKeyword, typeAEList, &descriptorList);
- if (err == noErr)
- {
- err = AECountItems(&descriptorList, &itemCount);
- if (err == noErr)
- {
- *numNodes = itemCount;
- *pReceiveNodeIDList = (FWXNodeID *) NewPtr(sizeof(FWXNodeID) * itemCount);
- if (*pReceiveNodeIDList == nil)
- err = memFullErr;
- }
-
- if (err == noErr)
- {
- for (i = 1; i <= itemCount; i++)
- {
- err = AEGetNthPtr(&descriptorList, i, typeChar, &keywd, &returnedType,
- p, 0, &actualSize);
- if (err == noErr)
- {
- p = NewPtr(actualSize + 1);
- if (p == nil)
- {
- err = memFullErr;
- break;
- }
-
- err = AEGetNthPtr(&descriptorList, i, typeChar, &keywd, &returnedType,
- p + 1, actualSize, &actualSize);
- p[0] = actualSize;
- err = GetNodeIDFromName(&((*pReceiveNodeIDList)[i-1]), (ConstStr255Param) p);
- DisposePtr(p);
- if (err != noErr)
- break;
- }
- }
- }
- AEDisposeDesc(&descriptorList);
- }
- return err;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // GetNodeIDFromName
- //
- // Get the node id from the node name.
- //
- static OSErr GetNodeIDFromName (
- FWXNodeID *nodeID,
- ConstStr255Param nodeName)
- {
- WindowDataPtr pWinData;
- RecvNodePtr pRecvNode;
- OSErr err = noErr;
-
- pWinData = (WindowDataPtr) GetWRefCon(gpFWXAppData->pSenderWindow);
- if (pWinData == nil)
- return memFullErr;
-
- *nodeID = kInvalidFWXNodeID;
- pRecvNode = pWinData->pRecvNodeList;
- while (pRecvNode != nil)
- {
- if (EqualString(nodeName, pRecvNode->nodeName, false, false))
- break;
- pRecvNode = pRecvNode->pNextNode;
- }
-
- if (pRecvNode == nil)
- err = qErr; // nodeName not found
- else
- *nodeID = pRecvNode->nodeID;
-
- return err;
- }
-
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // GetFSItemsFromEvent
- //
- // Get the list of items to be sent out of the appleevent.
- //
- static OSErr GetFSItemsFromEvent (
- FSSpecPtr *pSendItems,
- AppleEvent *pEvent,
- UInt16 *numItems)
- {
- Ptr p;
- FSSpecPtr pFileList;
- AEDescList descriptorList;
- AEKeyword keywd;
- DescType returnedType;
- Size actualSize;
- SInt32 itemCount;
- SInt32 i;
- OSErr err;
-
- err = AEGetParamDesc(pEvent, keyDirectObject, typeAEList, &descriptorList);
- if (err == noErr)
- {
- err = AECountItems(&descriptorList, &itemCount);
- pFileList = (FSSpecPtr) NewPtr(sizeof(FSSpec) * itemCount);
- if (pFileList == nil)
- err = memFullErr;
-
- if (err == noErr)
- {
- for (i = 1; i <= itemCount; i++)
- {
- err = AEGetNthPtr(&descriptorList, i, typeFSS, &keywd, &returnedType,
- &pFileList[i-1], sizeof(FSSpec), &actualSize);
- if (err == errAECoercionFail)
- {
- // try getting the data out as string and rolling our own fsspec
- err = AEGetNthPtr(&descriptorList, i, typeChar, &keywd, &returnedType,
- p, 0, &actualSize);
- if (err == noErr)
- {
- p = NewPtr(actualSize + 1);
- if (p != nil)
- {
- err = AEGetNthPtr(&descriptorList, i, typeChar, &keywd, &returnedType,
- p + 1, actualSize, &actualSize);
- if (err == noErr)
- {
- p[0] = actualSize;
- err = FSMakeFSSpec(0, 0, (ConstStr255Param) p, &pFileList[i-1]);
- }
- DisposePtr(p);
- }
- else
- {
- err = errAECoercionFail;
- break;
- }
- }
- }
- }
- }
- AEDisposeDesc(&descriptorList);
- *numItems = itemCount;
- *pSendItems = pFileList;
- }
- return err;
- }
-
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleOpenApplicationEvent
- //
- // Do nothing for now
- //
-
- static pascal OSErr HandleOpenApplicationEvent (
- AppleEvent *pAppleEvent,
- AppleEvent *pReplyEvent,
- SInt32 refCon)
- {
- return noErr;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleOpenDocumentsEvent
- //
- // Do nothing for now, eventually send a list of items
- //
-
- static pascal OSErr HandleOpenDocumentsEvent (
- AppleEvent *pAppleEvent,
- AppleEvent *pReplyEvent,
- SInt32 refCon)
- {
- return noErr;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleQuitApplicationEvent
- //
- // quit the app
- //
-
- static pascal OSErr HandleQuitApplicationEvent (
- AppleEvent *pAppleEvent,
- AppleEvent *pReplyEvent,
- SInt32 refCon)
- {
-
- HandleQuit(); // set the quit flag (doesn't immediately quit)
- return noErr;
- }
-
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // InstallAppleEventHandlers
- //
- // Setup handlers for standard open app, open documents, quit app
- // Also handler for when a user drops files on a FireWire Exchange node.
- //
-
- static OSErr InstallAppleEventHandlers (void)
- {
- OSErr err = noErr;
-
- // Create open application event handler.
- gpFWXAppData->openApplicationEventHandler =
- NewAEEventHandlerProc (HandleOpenApplicationEvent);
- if (gpFWXAppData->openApplicationEventHandler == nil)
- err = memFullErr;
-
- // Install handler.
- if (err == noErr) {
- err = AEInstallEventHandler
- (kCoreEventClass,
- kAEOpenApplication,
- gpFWXAppData->openApplicationEventHandler,
- (SInt32) gpFWXAppData,
- false);
- if (err == noErr) {
- gpFWXAppData->openApplicationEventHandlerInstalled =
- true;
- }
- }
-
- if (err == noErr) {
- // Create open documents event handler.
- gpFWXAppData->openDocumentsEventHandler =
- NewAEEventHandlerProc (HandleOpenDocumentsEvent);
- if (gpFWXAppData->openDocumentsEventHandler == nil)
- err = memFullErr;
-
- // Install handler.
- if (err == noErr) {
- err = AEInstallEventHandler
- (kCoreEventClass,
- kAEOpenDocuments,
- gpFWXAppData->openDocumentsEventHandler,
- (SInt32) gpFWXAppData,
- false);
- if (err == noErr) {
- gpFWXAppData->openDocumentsEventHandlerInstalled =
- true;
- }
- }
- }
-
- if (err == noErr) {
- // Create quit application event handler.
- gpFWXAppData->quitApplicationEventHandler =
- NewAEEventHandlerProc (HandleQuitApplicationEvent);
- if (gpFWXAppData->quitApplicationEventHandler == nil)
- err = memFullErr;
-
- // Install handler.
- if (err == noErr) {
- err = AEInstallEventHandler
- (kCoreEventClass,
- kAEQuitApplication,
- gpFWXAppData->quitApplicationEventHandler,
- (SInt32) gpFWXAppData,
- false);
- if (err == noErr) {
- gpFWXAppData->quitApplicationEventHandlerInstalled =
- true;
- }
- }
- }
-
- if (err == noErr) {
- // Create fsspec list handler.
- gpFWXAppData->fileSpecListHandler =
- NewAEEventHandlerProc (HandleAEFileSpecList);
- if (gpFWXAppData->fileSpecListHandler == nil)
- err = memFullErr;
-
- // Install handler.
- if (err == noErr) {
- err = AEInstallEventHandler
- (kAEFWXEventClass,
- kAEFileSpecList,
- gpFWXAppData->fileSpecListHandler,
- (SInt32) gpFWXAppData,
- false);
- if (err == noErr) {
- gpFWXAppData->fileSpecListHandlerInstalled =
- true;
- }
- }
- }
-
- // Create device added event handler.
- if (err == noErr) {
- gpFWXAppData->deviceAddedEventHandler =
- NewAEEventHandlerProc (HandleDeviceAddedEvent);
- if (gpFWXAppData->deviceAddedEventHandler == nil)
- err = memFullErr;
- }
-
- // Install handler.
- if (err == noErr) {
- err = AEInstallEventHandler
- (kAEFWXEventClass,
- kAEFWXDeviceAdded,
- gpFWXAppData->deviceAddedEventHandler,
- (SInt32) gpFWXAppData,
- false);
- if (err == noErr) {
- gpFWXAppData->deviceAddedEventHandlerInstalled =
- true;
- }
- }
-
- // Create device removed event handler.
- if (err == noErr)
- {
- gpFWXAppData->deviceRemovedEventHandler =
- NewAEEventHandlerProc (HandleDeviceRemovedEvent);
- if (gpFWXAppData->deviceRemovedEventHandler == nil)
- err = memFullErr;
- }
-
- // Install handler.
- if (err == noErr)
- {
- err = AEInstallEventHandler
- (kAEFWXEventClass,
- kAEFWXDeviceRemoved,
- gpFWXAppData->deviceRemovedEventHandler,
- (SInt32) gpFWXAppData,
- false);
- if (err == noErr)
- {
- gpFWXAppData->deviceRemovedEventHandlerInstalled =
- true;
- }
- }
-
- // Create device send items event handler.
- if (err == noErr)
- {
- gpFWXAppData->sendItemsEventHandler =
- NewAEEventHandlerProc (HandleSendItemsEvent);
- if (gpFWXAppData->sendItemsEventHandler == nil)
- err = memFullErr;
- }
-
- // Install handler.
- if (err == noErr)
- {
- err = AEInstallEventHandler
- (kAEFWXEventClass,
- kAEFWXSendEventID,
- gpFWXAppData->sendItemsEventHandler,
- (SInt32) gpFWXAppData,
- false);
- if (err == noErr)
- {
- gpFWXAppData->sendItemsEventHandlerInstalled =
- true;
- }
- }
-
- // Create get node list event handler.
- if (err == noErr)
- {
- gpFWXAppData->getNodeListEventHandler =
- NewAEEventHandlerProc (HandleGetNodeListEvent);
- if (gpFWXAppData->getNodeListEventHandler == nil)
- err = memFullErr;
- }
-
- // Install handler.
- if (err == noErr)
- {
- err = AEInstallEventHandler
- (kAEFWXEventClass,
- kAEFWXNodeListID,
- gpFWXAppData->getNodeListEventHandler,
- (SInt32) gpFWXAppData,
- false);
- if (err == noErr)
- {
- gpFWXAppData->getNodeListEventHandlerInstalled =
- true;
- }
- }
- return err;
- }
-
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // InstallCompletionRoutineProcs
- //
- // Setup handlers for our file read/write async completion procs
- // and FireWire read/write async completion procs
- //
-
- enum {
- uppIOCompletionProcWorkaroundInfo =
- kRegisterBased |
- RESULT_SIZE(SIZE_CODE(sizeof(OSErr))) |
- REGISTER_RESULT_LOCATION(kRegisterD0) |
- REGISTER_ROUTINE_PARAMETER(1, kRegisterA0, SIZE_CODE(sizeof(ParmBlkPtr)))
- };
-
- #define NewIOCompletionProcWorkaround(userRoutine) \
- (IOCompletionUPP) NewRoutineDescriptor((ProcPtr)(userRoutine), \
- uppIOCompletionProcWorkaroundInfo, \
- GetCurrentArchitecture())
-
- static OSErr InstallCompletionRoutineProcs (void)
- {
- OSErr err = noErr;
-
- gpFWXAppData->fileReadCompletionHandler =
- NewIOCompletionProcWorkaround(HandleFileReadComplete);
-
- if (gpFWXAppData->fileReadCompletionHandler == nil)
- err = memFullErr;
- else
- HoldMemory(gpFWXAppData->fileReadCompletionHandler, 32*1024); // JKL some large amount
-
- if (err == noErr) {
- gpFWXAppData->fileWriteCompletionHandler =
- NewIOCompletionProcWorkaround(HandleFileWriteComplete);
-
- if (gpFWXAppData->fileWriteCompletionHandler == nil)
- err = memFullErr;
- else
- HoldMemory(gpFWXAppData->fileWriteCompletionHandler, 32*1024); // JKL some large amount
-
- // while here go ahead and hold some other interrupt level stuff
- HoldMemory(HandleFWWriteComplete, 32*1024);
- HoldMemory(HandleFWReadComplete, 32*1024);
- HoldMemory(HandleFWControlComplete, 32*1024);
- HoldMemory(FWIXDequeue, 32*1024);
- HoldMemory(SetupFileReadPB, 32*1024);
- HoldMemory(&gFileReadQHdr, 32*1024);
- HoldMemory(&gFWControlQHdr, 32*1024);
- HoldMemory(&gReceiveQHdr, 32*1024);
- }
-
- return err;
-
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // DrawWindow
- //
- // Draws the space areas in the window
- //
- static void DrawWindow (
- WindowPtr pWin)
- {
- Handle hIconSuite;
- RgnHandle hOldClip;
- WindowDataPtr pWinData;
- StringPtr pString;
- Rect iconRect, textRect;
- Rect drawRect;
- SInt16 nodeIndex;
- OSErr err;
-
- pWinData = (WindowDataPtr) GetWRefCon(pWin);
- if (pWinData != nil)
- {
- GetClip(hOldClip = NewRgn());
- EraseRect(&pWin->portRect);
- DrawControls(pWin);
- DrawGrowIcon(pWin);
-
- SetOrigin (GetControlValue(pWinData->hHScrollBar),
- GetControlValue(pWinData->hVScrollBar));
- drawRect = pWin->portRect;
- drawRect.bottom -= kScrollBarAdjust;
- drawRect.right -= kScrollBarAdjust;
- ClipRect(&drawRect);
-
- err = GetIconSuite(&hIconSuite, kDropIconSuiteID, svAllAvailableData);
- for (nodeIndex = 1; nodeIndex <= pWinData->numRecvNodes; nodeIndex ++)
- {
- GetNodeDragRect(pWin, nodeIndex, &iconRect, &textRect);
- GetNodeName(pWin, nodeIndex, &pString);
- PlotIconSuite(&iconRect, atNone, ttNone, hIconSuite);
- InsetRect(&textRect, 1, 0);
- TETextBox(pString + 1, pString[0], &textRect, teJustLeft);
- }
- DisposeIconSuite(hIconSuite, false);
- SetOrigin(0, 0);
- SetClip(hOldClip);
- DisposeRgn(hOldClip);
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // CreateMenus
- //
- // Make our menu bar. We only do about, open, and quit.
- //
-
- static OSErr CreateMenus()
- {
- OSErr err = noErr;
- Handle hMenuBarResource;
-
- hMenuBarResource = GetNewMBar (kMenuBarResourceID);
- if (hMenuBarResource != nil)
- {
- SetMenuBar (hMenuBarResource);
- DisposeHandle (hMenuBarResource);
- AppendResMenu (GetMenuHandle (kAppleMenuID), 'DRVR');
- DrawMenuBar ();
- } else
- err = resNotFound;
-
- return err;
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleCloseWindow
- //
- // Disposes of a window and its refcon memory.
- //
-
- static void HandleCloseWindow (
- WindowPtr pWin)
- {
- WindowDataPtr pWinData;
- OpenNodePtr pTempOpenNode;
- RecvNodePtr pTempRecvNode;
-
- if (pWin != nil) {
- pWinData = (WindowDataPtr) GetWRefCon(pWin);
- if (pWinData != nil) {
-
- // dispose of the receive node records
- pTempRecvNode = pWinData->pRecvNodeList;
- while (pTempRecvNode != nil)
- {
- if (pTempRecvNode->pReplyTimer != nil)
- {
- if (pTempRecvNode->pReplyTimer->timerTask.tmAddr != nil)
- DisposeRoutineDescriptor(pTempRecvNode->pReplyTimer->timerTask.tmAddr);
- DisposePtr((Ptr) pTempRecvNode->pReplyTimer);
- }
-
- pTempRecvNode = (RecvNodePtr) pTempRecvNode->pNextNode;
- DisposePtr((Ptr) pWinData->pRecvNodeList);
- pWinData->pRecvNodeList = pTempRecvNode;
- }
-
- // dispose of the open node records
- pTempOpenNode = pWinData->pOpenNodeList;
- while (pTempOpenNode != nil)
- {
- CloseFWXNode(pTempOpenNode->nodeID);
-
- pTempOpenNode = (OpenNodePtr) pTempOpenNode->pNextNode;
- DisposePtr((Ptr) pWinData->pOpenNodeList);
- pWinData->pOpenNodeList = pTempOpenNode;
- }
-
- DisposePtr((Ptr) pWinData);
- DisposeWindow(pWin);
- }
- }
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleQuit
- //
- // Quit the app
- //
-
- static void HandleQuit (void)
- {
- SInt16 itemHit;
-
- if (gSendingFile || gCheckingTransfer) {
- // JKL *** does ok button work right? Complete and then quit
- // is background processing correct?
- itemHit = HandleCautionAlert(kCantQuitAlertID);
- if (itemHit == kCancelButton) { // the abort and quit button
- StopTransfer(nil);
- gpFWXAppData->quitFlag = true;
- }
- } else {
- gpFWXAppData->quitFlag = true;
- }
-
- if (gpFWXAppData->quitFlag) {
- SendFWXQuit();
- if (gpFWXAppData->pNotifyRec->nmRefCon == kNotificationPosted)
- NMRemove(gpFWXAppData->pNotifyRec);
-
- if (gpFWXAppData->pSenderWindow != nil)
- HandleCloseWindow(gpFWXAppData->pSenderWindow);
- RemoveDragHandlers();
- }
-
- }
-
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleMenuCommand
- //
- // handle repsonses to user menu selections
- //
-
- static void HandleMenuCommand(
- SInt32 menuCommand)
- {
- VersRecHndl h;
- WindowPtr pWin;
- UInt8 *vers;
- SInt16 menuID;
- SInt16 menuItem;
- Str255 deskAccessoryName;
-
- menuID = menuCommand >> 16;
- menuItem = menuCommand & 0xFFFF;
-
- switch (menuID)
- {
- case kAppleMenuID:
- if (menuItem == kAboutMenuItem)
- {
- h = (VersRecHndl) Get1Resource('vers', 1);
- vers = (**h).shortVersion;
- vers += (vers[0] + 1);
- ParamText(vers, "\p", "\p", "\p");
- HandleAlert(kAboutAlertResourceID);
- }
- else
- {
- GetMenuItemText(GetMenuHandle(kAppleMenuID), menuItem, deskAccessoryName);
- OpenDeskAcc(deskAccessoryName);
- }
- break;
-
- case kFileMenuID:
- switch (menuItem)
- {
- case kOpenMenuItem:
- pWin = gpFWXAppData->pSenderWindow;
- ShowWindow(pWin);
- EnableItem(GetMenuHandle(kFileMenuID), kCloseMenuItem);
- DisableItem(GetMenuHandle(kFileMenuID), kOpenMenuItem);
- EnableItem(GetMenuHandle(kViewMenuID), 0);
- DrawMenuBar();
- break;
- case kCloseMenuItem:
- pWin = gpFWXAppData->pSenderWindow;
- HideWindow(pWin);
- EnableItem(GetMenuHandle(kFileMenuID), kOpenMenuItem);
- DisableItem(GetMenuHandle(kFileMenuID), kCloseMenuItem);
- DisableItem(GetMenuHandle(kViewMenuID), 0);
- DrawMenuBar();
- break;
- case kOpenDropMenuItem:
- OpenDropFolder();
- break;
- case kQuitMenuItem:
- HandleQuit();
- break;
- }
- break;
- case kEditMenuID:
- switch (menuItem)
- {
- case kPrefsMenuItem:
- HandlePrefsDialog();
- break;
- }
- case kViewMenuID:
- switch (menuItem)
- {
- case kSmallIconMenuItem:
- case kIconMenuItem:
- HandleViewSize(menuItem);
- break;
- }
- break;
- }
- HiliteMenu(0); // unhilight menu title
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // OpenDropFolder
- //
- // Send an appleevent to the finder to open our drop folder
- //
- OSErr OpenDropFolder(void)
- {
- AppleEvent send;
- AppleEvent reply;
- AEDesc target;
- ProcessSerialNumber psn;
- ProcessInfoRecPtr pInfo;
- SInt32 dirID;
- OSErr err;
-
- psn.highLongOfPSN = kNoProcess;
- psn.lowLongOfPSN = kNoProcess;
- pInfo = (ProcessInfoRecPtr) NewPtr(sizeof(ProcessInfoRec));
- if (pInfo == nil)
- return memFullErr;
- pInfo->processInfoLength = sizeof(ProcessInfoRec);
- pInfo->processName = nil;
- pInfo->processAppSpec = nil;
- do {
- err = GetNextProcess(&psn);
- GetProcessInformation(&psn, pInfo);
- }
- while ((err == noErr) && (pInfo->processType != 'FNDR'));
- DisposePtr((Ptr) pInfo);
-
- err = FSpDirCreate(&gpFWXAppData->fwixReceiveFolder, smSystemScript, &dirID);
- if (err == dupFNErr)
- err = noErr;
-
- if (err == noErr) {
-
- err = AECreateDesc(typeProcessSerialNumber, (Ptr) &psn, sizeof(ProcessSerialNumber), &target);
- if (err != noErr)
- return err;
-
- err = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &target,
- kAutoGenerateReturnID, kAnyTransactionID, &send);
-
- err = AEPutParamPtr(&send, keyDirectObject, typeFSS, (Ptr) &(gpFWXAppData->fwixReceiveFolder), sizeof(FSSpec));
-
- err = AESend(&send, &reply, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer,
- kAENormalPriority, kAEDefaultTimeout, nil, nil);
-
- AEDisposeDesc(&target);
- AEDisposeDesc(&send);
- }
- SetFrontProcess(&psn);
-
- return err;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // OpenSharingSetup
- //
- // Send an appleevent to the finder to open the Sharing Setup Control Panel
- //
- static OSErr OpenSharingSetup(void)
- {
- AppleEvent send;
- AppleEvent reply;
- AEDesc target;
- ProcessSerialNumber psn;
- ProcessInfoRecPtr pInfo;
- FSSpec spec;
- SInt32 dirID;
- SInt16 vRefNum;
- OSErr err;
-
- psn.highLongOfPSN = kNoProcess;
- psn.lowLongOfPSN = kNoProcess;
- pInfo = (ProcessInfoRecPtr) NewPtr(sizeof(ProcessInfoRec));
- if (pInfo == nil)
- return memFullErr;
- pInfo->processInfoLength = sizeof(ProcessInfoRec);
- pInfo->processName = nil;
- pInfo->processAppSpec = nil;
- do {
- err = GetNextProcess(&psn);
- GetProcessInformation(&psn, pInfo);
- }
- while ((err == noErr) && (pInfo->processType != 'FNDR'));
- DisposePtr((Ptr) pInfo);
-
- if (err == noErr) {
-
- err = AECreateDesc(typeProcessSerialNumber, (Ptr) &psn, sizeof(ProcessSerialNumber), &target);
- if (err != noErr)
- return err;
-
- err = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &target,
- kAutoGenerateReturnID, kAnyTransactionID, &send);
-
- err = FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder,
- &vRefNum, &dirID);
- err = FSMakeFSSpec(vRefNum, dirID, "\pSharing Setup", &spec);
- if (err == fnfErr)
- err = FSMakeFSSpec(vRefNum, dirID, "\pFile Sharing", &spec);
- err = AEPutParamPtr(&send, keyDirectObject, typeFSS, (Ptr) &spec, sizeof(FSSpec));
-
- err = AESend(&send, &reply, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer,
- kAENormalPriority, kAEDefaultTimeout, nil, nil);
-
- AEDisposeDesc(&target);
- AEDisposeDesc(&send);
- }
- SetFrontProcess(&psn);
-
- return err;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleViewSize
- //
- // Handle the window view size command.
- //
- static OSErr HandleViewSize(
- SInt16 whichMenuItem)
- {
- MenuHandle hMenu;
- WindowPtr pWin;
- WindowDataPtr pWinData;
- OSErr err = noErr;
-
- pWin = gpFWXAppData->pSenderWindow;
- pWinData = (WindowDataPtr) GetWRefCon(pWin);
- if (pWinData == nil)
- return memFullErr;
-
- hMenu = GetMenuHandle(kViewMenuID);
- if ((pWinData->windowView == kLargeIconView) && (whichMenuItem == kSmallIconMenuItem))
- {
- pWinData->windowView = kSmallIconView;
- gpFWXAppData->fwixPrefs |= kIconView;
- SetItemMark(hMenu, kSmallIconMenuItem, checkMark);
- SetItemMark(hMenu, kIconMenuItem, noMark);
- AdjustNodeIcons(pWinData, &pWin->portRect);
- AdjustScrollBars(pWin, false);
- UpdatePrefsFile();
- }
- else if ((pWinData->windowView == kSmallIconView) && (whichMenuItem == kIconMenuItem))
- {
- pWinData->windowView = kLargeIconView;
- gpFWXAppData->fwixPrefs &= ~kIconView;
- SetItemMark(hMenu, kSmallIconMenuItem, noMark);
- SetItemMark(hMenu, kIconMenuItem, checkMark);
- AdjustNodeIcons(pWinData, &pWin->portRect);
- AdjustScrollBars(pWin, false);
- UpdatePrefsFile();
- }
- InvalRect(&pWin->portRect);
- return err;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleMouseDownEvent
- //
- // This routine handles mouse down events.
- //
- static void HandleMouseDownEvent(
- EventRecord *pEventRecord)
- {
- static ControlActionUPP cupp;
- GrafPtr curPort;
- WindowRef theWindow;
- Rect tempRect;
- ControlHandle hControl;
- Point mouse;
- SInt16 whichPart;
- SInt16 cntlPart;
-
- whichPart = FindWindow(pEventRecord->where, &theWindow);
-
- switch (whichPart) {
- case inSysWindow: // desk accessory window
- SystemClick(pEventRecord, theWindow);
- break;
-
- case inMenuBar:
- HandleMenuCommand(MenuSelect(pEventRecord->where));
- break;
-
- case inDrag:
- tempRect = qd.screenBits.bounds;
- if (gpFWXAppData->pSenderWindow != FrontWindow())
- {
- // progress bar would be up, it is movable modal
- if (theWindow == FrontWindow())
- DragWindow(theWindow, pEventRecord->where, &tempRect);
- else
- SysBeep(0);
- } else
- DragWindow(theWindow, pEventRecord->where, &tempRect);
- SaveWindowPos(theWindow);
- break;
-
- case inGoAway:
- if (TrackGoAway(theWindow, pEventRecord->where)) {
- HideWindow(theWindow);
- EnableItem(GetMenuHandle(kFileMenuID), kOpenMenuItem);
- DisableItem(GetMenuHandle(kFileMenuID), kCloseMenuItem);
- DisableItem(GetMenuHandle(kViewMenuID), 0);
- DrawMenuBar();
- }
- break;
-
- case inGrow:
- HandleGrowWindow(theWindow, pEventRecord->where);
- break;
-
- case inZoomIn:
- case inZoomOut:
- if (TrackBox(theWindow, pEventRecord->where, whichPart))
- HandleZoomWindow(theWindow, whichPart);
- break;
-
- case inContent:
- GetPort(&curPort);
- SetPort(theWindow);
- mouse = pEventRecord->where;
- GlobalToLocal(&mouse);
- cntlPart = FindControl(mouse, theWindow, &hControl);
- if (cntlPart)
- {
- if (gpFWXAppData->pSenderWindow == FrontWindow())
- {
- // handle user click in one of the sender window scroll bars
- if (cntlPart == kControlIndicatorPart)
- HandleThumbScroll(hControl, theWindow, mouse);
- else
- {
- if (!cupp)
- cupp = NewControlActionProc(HandleScrollAction);
- TrackControl(hControl, mouse, cupp);
- }
- }
- else
- { // progress bar is up, make it modal, check for stop button
- if (theWindow != FrontWindow())
- SysBeep(0);
- else
- {
- if (cntlPart == kControlButtonPart)
- {
- if (TrackControl(hControl, mouse, nil))
- StopTransfer(nil);
- }
- }
- }
- }
- SetPort(curPort);
- break;
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleThumbScroll
- //
- // Handle scrolling by the thumb button.
- //
- static pascal void HandleThumbScroll (
- ControlHandle scrollCtl,
- WindowPtr pWin,
- Point mouse)
- {
- WindowDataPtr pWinData = (WindowDataPtr) GetWRefCon(pWin);
- RgnHandle updateRgn;
- Rect drawRect;
- SInt16 value, h, v;
- SInt16 cntlPart;
-
- value = GetControlValue(scrollCtl);
- cntlPart = TrackControl(scrollCtl, mouse, nil);
- if (cntlPart)
- {
- value -= GetControlValue(scrollCtl);
- if (value != 0)
- {
- h = v = 0;
- if (scrollCtl == pWinData->hVScrollBar)
- v = value;
- else
- h = value;
-
- drawRect = pWin->portRect;
- drawRect.bottom -= kScrollBarAdjust;
- drawRect.right -= kScrollBarAdjust;
- ScrollRect(&drawRect, h, v, updateRgn = NewRgn());
- InvalRgn(updateRgn);
- DisposeRgn(updateRgn);
- BeginUpdate(pWin);
- DrawWindow(pWin);
- EndUpdate(pWin);
- }
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleScrollAction
- //
- // Handle scrolling window.
- //
- static pascal void HandleScrollAction (
- ControlHandle scrollCtl,
- SInt16 part)
- {
- WindowPtr pWin = gpFWXAppData->pSenderWindow;
- WindowDataPtr pWinData = (WindowDataPtr) GetWRefCon(pWin);
- RgnHandle updateRgn;
- Rect drawRect;
- SInt16 delta, value, h, v;
- SInt16 oldValue, max;
-
- if ((pWinData != nil) && part)
- {
- switch (part)
- {
- case kControlUpButtonPart:
- case kControlDownButtonPart:
- delta = 5;
- break;
-
- case kControlPageUpPart:
- case kControlPageDownPart:
- if (scrollCtl == pWinData->hHScrollBar)
- delta = pWin->portRect.right - pWin->portRect.left - kScrollBarAdjust;
- else
- delta = pWin->portRect.bottom - pWin->portRect.top - kScrollBarAdjust;
- break;
-
- // handle cases generated by keyboard input
- case kPageUpKey:
- case kPageDownKey:
- delta = pWin->portRect.bottom - pWin->portRect.top - kScrollBarAdjust;
- break;
-
- case kHomeKey:
- delta = -32000; // a large enough number to make value = 0
- break;
-
- case kEndKey:
- delta = 32000; // a large enough number to make value = max
- break;
- }
-
- // reverse direction for scrolling up
- if ((part == kControlUpButtonPart) || (part == kControlPageUpPart) || (part == kPageUpKey))
- delta = -delta;
-
- // pin scroll value at min or max.
- oldValue = GetControlValue(scrollCtl);
- value = oldValue + delta;
- if (value < 0)
- value = 0;
- max = GetControlMaximum(scrollCtl);
- if (value > max)
- value = max;
-
- if (value != oldValue)
- {
- drawRect = pWin->portRect;
- drawRect.bottom -= kScrollBarAdjust;
- drawRect.right -= kScrollBarAdjust;
- SetControlValue(scrollCtl, value);
- h = oldValue - value;
- v = 0;
- if (scrollCtl == pWinData->hVScrollBar)
- {
- v = h;
- h = 0;
- }
- ScrollRect(&drawRect, h, v, updateRgn = NewRgn());
- InvalRgn(updateRgn);
- DisposeRgn(updateRgn);
- BeginUpdate(pWin);
- DrawWindow(pWin);
- EndUpdate(pWin);
- }
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleZoomWindow
- //
- // Zoom the window.
- //
- static void HandleZoomWindow(
- WindowPtr pWin,
- SInt16 whichZoom)
- {
- WindowDataPtr pWinData;
- Rect zoomRect;
- SInt16 vSize, hSize;
- SInt16 row, column;
-
- if (whichZoom == inZoomOut)
- {
- pWinData = (WindowDataPtr) GetWRefCon(pWin);
- if (pWinData != nil)
- {
- if (pWinData->windowView == kLargeIconView)
- {
- row = pWinData->numRecvNodes % kLargeRowCount;
- column = pWinData->numRecvNodes / kLargeRowCount;
- if (column == 0)
- vSize = row * kLargeVSize;
- else
- vSize = kLargeRowCount * kLargeVSize;
- hSize = (column + 1) * kLargeHSize;
- }
- else
- {
- row = pWinData->numRecvNodes % kSmallRowCount;
- column = pWinData->numRecvNodes / kSmallRowCount;
- if (column == 0)
- vSize = row * kSmallVSize;
- else
- vSize = kSmallRowCount * kSmallVSize;
- // make sure there is enough room for scroll bars
- if (vSize < kLargeVSize)
- vSize = kLargeVSize;
- hSize = (column + 1) * kSmallHSize;
- }
- }
- zoomRect = (**((WStateDataHandle) ((WindowPeek) pWin)->dataHandle)).userState;
- zoomRect.bottom = zoomRect.top + vSize + kScrollBarWidth;
- zoomRect.right = zoomRect.left + hSize + kScrollBarWidth;
- (**((WStateDataHandle) ((WindowPeek) pWin)->dataHandle)).stdState = zoomRect;
- }
- ZoomWindow(pWin, whichZoom, FrontWindow() == pWin);
- AdjustScrollBars(pWin, true);
- InvalRect(&pWin->portRect);
- SaveWindowPos(pWin);
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleGrowWindow
- //
- // Grow the window.
- //
- static void HandleGrowWindow(
- WindowPtr pWin,
- Point clickLoc)
- {
- Rect limitRect;
- SInt32 growSize;
-
- SetRect(&limitRect, kLargeVSize, kLargeVSize, 3000, 3000);
- growSize = GrowWindow(pWin, clickLoc, &limitRect);
- if (growSize != 0)
- {
- SizeWindow(pWin, LoWord(growSize), HiWord(growSize), true);
- AdjustScrollBars(pWin, true);
- InvalRect(&pWin->portRect);
- SaveWindowPos(pWin);
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // StopTransfer
- //
- // Handle user stopping file transfer.
- //
- void StopTransfer (
- RecvNodePtr pNode)
- {
- FWXPacketPtr pPktInfo; // puts a packet structure on ioBuffer
- IOParamPtr pIOPb; // write parameter block
- TxFSSpecPtr pTxItem;
- NodeSendItemPtr pSendItem, pTempItem;
-
- if (pNode != nil)
- {
- if (pNode->pTxItemList != nil)
- {
- pSendItem = pNode->pTxItemList;
- while (pSendItem != nil)
- {
- pTempItem = pSendItem;
- pSendItem = pSendItem->pNextSendItem;
- DisposePtr((Ptr) pTempItem);
- }
- pNode->pTxItemList = nil;
- }
- }
-
- if (gSendingFile || gCheckingTransfer)
- {
- gCheckingTransfer = false;
- gForkComplete = true;
- gForkWriteComplete = true;
- CleanupCopyDialog(FrontWindow());
- if (gSendingDataFork || gSendingResFork)
- {
- gSendingDataFork = false;
- gSendingResFork = false;
- FSClose(gCurForkRefNum);
- }
-
- if (gSendingFile)
- {
- gSendingFile = false;
-
- // send file stopped to receiver
- FWIXDequeue ((QElemPtr *) &pIOPb, &gFWControlQHdr);
- if (pIOPb != nil)
- {
- SetupFWControlPB(pIOPb);
-
- pPktInfo = (FWXPacketPtr) pIOPb->ioBuffer;
- pPktInfo->packetType = kTransferStopped;
- pIOPb->ioMisc = (Ptr) kTransferStopped;
- pIOPb->ioReqCount = 4; // just packet header
-
- CallFWXNode(pIOPb);
- }
- }
- }
-
- // dequeue and dispose of all file queue entries
- pTxItem = (TxFSSpecPtr) gSendQHdr.qHead;
- while (pTxItem != nil)
- {
- Dequeue((QElemPtr) pTxItem, &gSendQHdr);
- DisposePtr((Ptr) pTxItem->pFSSpec);
- DisposePtr((Ptr) pTxItem);
-
- // get another queue item
- pTxItem = (TxFSSpecPtr) gSendQHdr.qHead;
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleUpdateEvent
- //
- // This routine handles update events.
- //
- void HandleUpdateEvent (
- EventRecord *pEventRecord)
- {
- WindowPtr pWin;
- GrafPtr curPort;
-
- pWin = (WindowPtr) pEventRecord->message;
-
- GetPort(&curPort);
- SetPort(pWin);
- if (pWin == gpFWXAppData->pSenderWindow)
- {
- BeginUpdate(pWin);
- DrawWindow(pWin);
- EndUpdate(pWin);
- }
- else
- {
- BeginUpdate(pWin);
- UpdateDialog(pWin, pWin->visRgn);
- EndUpdate(pWin);
- }
- SetPort(curPort);
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleKeyEvent
- //
- // This routine handles key events. The only ones we handle are menu shortcuts
- // and stopping the transfer shortcuts.
- //
- static void HandleKeyEvent (
- EventRecord *pEventRecord)
- {
- WindowPtr pWin;
- WindowDataPtr pWinData;
- char key;
-
- key = pEventRecord->message & charCodeMask;
-
- if ((pEventRecord->modifiers & cmdKey) && (pEventRecord->what == keyDown))
- {
- if ((gSendingFile || gCheckingTransfer) && (key == kPeriodKey))
- StopTransfer(nil);
- else
- HandleMenuCommand(MenuKey(key));
- }
-
- if ((gSendingFile || gCheckingTransfer) && (key == kEscapeKey))
- StopTransfer(nil);
-
- if ((key == kPageUpKey) || (key == kPageDownKey) || (key == kHomeKey) || (key == kEndKey))
- {
- pWin = FrontWindow();
- if (pWin == gpFWXAppData->pSenderWindow)
- {
- pWinData = (WindowDataPtr) GetWRefCon(pWin);
- HandleScrollAction(pWinData->hVScrollBar, key);
- }
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // HandleOSEvent
- //
- // This routine handles OS events.
- //
-
- static void HandleOSEvent(
- EventRecord *pEventRecord)
- {
- UInt32 osEventType;
-
- // Get the OS event type.
- osEventType = pEventRecord->message >> 24;
-
- switch (osEventType)
- {
- case suspendResumeMessage:
- if (pEventRecord->message & resumeFlag)
- {
- gpFWXAppData->inForeground = true;
-
- if (gpFWXAppData->pNotifyRec->nmRefCon == kNotificationPosted)
- {
- NMRemove(gpFWXAppData->pNotifyRec);
- gpFWXAppData->pNotifyRec->nmRefCon = kNoNotificationPosted;
- }
- }
- else
- gpFWXAppData->inForeground = false;
-
- HandleActivate(FrontWindow(), gpFWXAppData->inForeground);
- break;
- }
- }
-
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleEvent
- //
- // main event handling routine
- //
-
- static void HandleEvent (
- EventRecord *theEvent)
- {
- Point point;
-
- switch (theEvent->what) {
- case mouseDown:
- HandleMouseDownEvent(theEvent);
- break;
-
- case updateEvt:
- HandleUpdateEvent(theEvent);
- break;
-
- case keyDown:
- case autoKey:
- HandleKeyEvent(theEvent);
- break;
-
- case kHighLevelEvent:
- AEProcessAppleEvent(theEvent);
- break;
-
- case osEvt:
- HandleOSEvent(theEvent);
- break;
-
- case diskEvt:
- if ((theEvent->message >> 16) != noErr) {
- SetPt (&point, kDILeft, kDITop);
- DIBadMount (point, theEvent->message);
- }
- break;
-
- case activateEvt:
- HandleActivate((WindowPtr) theEvent->message, (theEvent->modifiers & activeFlag) != 0);
- break;
- }
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleActivate
- //
- // Take care of activating/deactivating, mainly just handling controls
- // and grow box.
- //
- static void HandleActivate(
- WindowPtr pWin,
- Boolean becomingActive)
- {
- WindowDataPtr pWinData;
-
- if (IsAppWindow(pWin))
- {
- if (becomingActive)
- {
- gpFWXAppData->inForeground = true;
- if (pWin == gpFWXAppData->pSenderWindow)
- {
- pWinData = (WindowDataPtr) GetWRefCon(pWin);
- ShowControl(pWinData->hHScrollBar);
- ShowControl(pWinData->hVScrollBar);
- }
- InvalRect(&pWin->portRect);
- }
- else
- {
- gpFWXAppData->inForeground = false;
- if (pWin == gpFWXAppData->pSenderWindow)
- {
- pWinData = (WindowDataPtr) GetWRefCon(pWin);
- HideControl(pWinData->hHScrollBar);
- HideControl(pWinData->hVScrollBar);
- DrawGrowIcon(pWin);
- }
- }
- }
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // IsAppWindow
- //
- // Check to see if our window needs handling for activate event.
- //
- static Boolean IsAppWindow(
- WindowPtr pWin)
- {
- SInt16 windowKind;
-
- if (pWin == nil)
- return false;
- else {
- windowKind = ((WindowPeek) pWin)->windowKind;
- return (windowKind == userKind);
- }
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleIdle
- //
- // At idle time check for processing send/receive information that may be
- // halted.
- //
- void HandleIdle(void)
- {
- RecvNodePtr pRecvNode;
- CurFileInfoPtr pTxInfo;
- OSErr err;
-
- // received some data, handle it
- if (gReceiveQHdr.qHead != nil) {
- HandleReceive();
- }
-
- if (gSendingFile)
- UpdateProgressBar(FrontWindow(), kProgressBarUserItem);
-
- // if a file is being sent and end of file has been read, handle it
- if (gSendingDataFork || gSendingResFork)
- {
- if (gForkComplete && gForkWriteComplete)
- {
- HandleForkReadComplete();
- }
- }
-
- // if sending errors, show the alert and other routines
- // will take care of the clean up
- // JKL *** you sure about this?, no!
- if (gSendError != 0) {
- gSendError = (FWXNodeID) 0;
- err = GetNodeInfo(gSendError, &pRecvNode);
- if (err == noErr) {
- ParamText(pRecvNode->nodeName, "\p","\p","\p");
- HandleCautionAlert(kCommunicationErrorAlertID);
- }
- }
-
- // if receiving error show the alert and also clean up
- // pending receive
- if (gReceiveError != 0) {
- gReceiveError = (FWXNodeID) 0;
- err = GetCurFileInfo(gReceiveError, &pTxInfo, false);
- if (err == noErr)
- HandleStopTransfer(pTxInfo);
- err = GetNodeInfo(gSendError, &pRecvNode);
- if (err == noErr) {
- ParamText(pRecvNode->nodeName, "\p","\p","\p");
- HandleCautionAlert(kCommunicationErrorAlertID);
- }
- }
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleCautionAlert
- //
- // Routine to call caution alerts
- //
- SInt16 HandleCautionAlert (
- SInt16 alertResID)
- {
- UniversalProcPtr alertFilterProc = NewModalFilterProc(HandleAlertEventFilter);
- SInt16 itemHit;
-
- itemHit = CautionAlert(alertResID, alertFilterProc);
- DisposeRoutineDescriptor(alertFilterProc);
- return itemHit;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleStopAlert
- //
- // Routine to call stop alerts
- //
- SInt16 HandleStopAlert (
- SInt16 alertResID)
- {
- UniversalProcPtr alertFilterProc = NewModalFilterProc(HandleAlertEventFilter);
- SInt16 itemHit;
-
- itemHit = StopAlert(alertResID, alertFilterProc);
- DisposeRoutineDescriptor(alertFilterProc);
- return itemHit;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleNoteAlert
- //
- // Routine to call note alerts
- //
- SInt16 HandleNoteAlert (
- SInt16 alertResID)
- {
- UniversalProcPtr alertFilterProc = NewModalFilterProc(HandleAlertEventFilter);
- SInt16 itemHit;
-
- itemHit = NoteAlert(alertResID, alertFilterProc);
- DisposeRoutineDescriptor(alertFilterProc);
- return itemHit;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleAlert
- //
- // Routine to call alerts
- //
- SInt16 HandleAlert (
- SInt16 alertResID)
- {
- UniversalProcPtr alertFilterProc = NewModalFilterProc(HandleAlertEventFilter);
- SInt16 itemHit;
-
- itemHit = Alert(alertResID, alertFilterProc);
- DisposeRoutineDescriptor(alertFilterProc);
- return itemHit;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleAlertEventFilter
- //
- // Handles alert filter events
- //
- static pascal Boolean HandleAlertEventFilter(
- DialogPtr pDlog,
- EventRecord *pEvent,
- SInt16 *itemHit)
- {
- Handle item;
- Rect box;
- UInt32 finalTicks;
- SInt16 itemType;
- UInt8 key;
- Boolean handledIt;
-
- handledIt = false;
-
- switch (pEvent->what)
- {
- case keyDown:
- key = pEvent->message & charCodeMask;
-
- if (key == kReturnKey || key == kEnterKey)
- {
- GetDialogItem(pDlog, kOKButton, &itemType, &item, &box);
- HiliteControl((ControlHandle) item, kControlButtonPart);
- Delay(8, &finalTicks);
- HiliteControl((ControlHandle) item, 0);
- handledIt = true;
- *itemHit = kOKButton;
- }
- break;
-
- case nullEvent:
- HandleIdle();
- break;
-
- case updateEvt:
- HandleUpdateEvent(pEvent);
- break;
- }
- return handledIt;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // FWXMain
- //
- // main routine for FWiX
- //
- #ifdef __MWERKS__
- void main()
- #else
- void FWXMain()
- #endif
- {
- EventRecord theEvent;
- OSErr err = noErr;
- Boolean gotEvent;
-
- err = FWXInit();
-
- if (err == noErr)
- {
- do
- {
- // Get any FWX events.
- GetNextFWXClientEvent (gpFWXAppData->fwxClientID);
-
- gotEvent = WaitNextEvent(everyEvent, &theEvent, 0, nil);
-
- if (gotEvent)
- HandleEvent(&theEvent);
- else
- HandleIdle();
-
- } while (!gpFWXAppData->quitFlag);
- }
- else if (err == memFullErr)
- {
- HandleStopAlert(kMemoryErrorAlertID);
- }
-
- FWXDispose ();
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // SetupFWXNode
- //
- // Register with the fam and get any currently connected nodes
- //
- static OSErr SetupFWXNode(void)
- {
- UInt32 numFWXNodes;
- UInt32 nodeNum;
- FWXNodeID *nodeIDList = nil;
- OSErr err = noErr;
-
- err = RegisterFWXClientApplication(
- &(gpFWXAppData->fwxClientID),
- (UInt32) gpFWXAppData);
-
- // Get list of nodes.
- //zzz theoretically, new ones can be added while we're doing this.
- if (err == noErr) {
- err = GetFWXNodeList (nil, 0, &numFWXNodes);
- }
-
- if ((err == noErr) && (numFWXNodes > 0)) {
- nodeIDList = (FWXNodeID *) NewPtr(numFWXNodes * sizeof(FWXNodeID));
- if (nodeIDList != nil) {
- err = GetFWXNodeList(nodeIDList, numFWXNodes, &numFWXNodes);
- } else
- err = memFullErr;
- }
-
- if (err == noErr) {
- for (nodeNum = 0; nodeNum < numFWXNodes; nodeNum++) {
- SendDeviceAddedToSelf(nodeIDList[nodeNum]);
- }
- }
-
- if (nodeIDList != nil)
- DisposePtr((Ptr) nodeIDList);
-
- return err;
- }
-
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // SetupIOQueue
- //
- // Setup the queueus. Initialize everything. Allocate buffers for reading
- // from files and writing to FireWire. The idea is to read then use the same
- // parameter block to write. The used queue is for taking a parameter block
- // and putting back on the free queue.
- //
- static OSErr SetupIOQueue(void)
- {
- IOParamPtr pIOPb;
- SInt16 i;
- OSErr err = noErr;
-
- // init all of the queue headers
- gSendQHdr.qHead = nil;
- gSendQHdr.qTail = nil;
-
- gAESendQHdr.qHead = nil;
- gAESendQHdr.qTail = nil;
-
- gFileReadQHdr.qHead = nil;
- gFileReadQHdr.qTail = nil;
-
- gFWControlQHdr.qHead = nil;
- gFWControlQHdr.qTail = nil;
-
- gReceiveQHdr.qHead = nil;
- gReceiveQHdr.qTail = nil;
-
- gCurFileQHdr.qHead = nil;
- gCurFileQHdr.qTail = nil;
-
- queuesInitialized = true;
-
- // create file read parameter blocks and queue them on the file read queue
- for (i=0; i < kFileReadBufs; i++) {
- pIOPb = (IOParamPtr) NewPtr(sizeof(IOParam));
- if (pIOPb != nil) {
- HoldMemory ((Ptr) pIOPb, sizeof(IOParam));
- } else {
- FWDebugStr("\pOut of memory, SetupIOQ");
- err = memFullErr;
- break;
- }
-
- // allocate data buffer
- pIOPb->ioBuffer = NewPtr(kFileReadBufSize);
- if (pIOPb->ioBuffer != nil) {
- HoldMemory (pIOPb->ioBuffer, kFileReadBufSize);
- } else {
- UnholdMemory ((Ptr) pIOPb, sizeof(IOParam));
- DisposePtr((Ptr) pIOPb);
- FWDebugStr("\pOut of memory, SetupIOQ");
- err = memFullErr;
- break;
- }
-
- Enqueue((QElemPtr) pIOPb, &gFileReadQHdr);
- }
-
- // create FW control parameter blocks and queue them on the FW control queue
- for (i=0; i < kFWControlParams; i++) {
- pIOPb = (IOParamPtr) NewPtr(sizeof(IOParam));
- if (pIOPb != nil) {
- HoldMemory ((Ptr) pIOPb, sizeof(IOParam));
- } else {
- FWDebugStr("\pOut of memory, SetupIOQ");
- err = memFullErr;
- break;
- }
-
- pIOPb->ioBuffer = NewPtr(kFWControlParamBufferSize);
- if (pIOPb->ioBuffer != nil) {
- HoldMemory (pIOPb->ioBuffer, kFWControlParamBufferSize);
- } else {
- UnholdMemory ((Ptr) pIOPb, sizeof(IOParam));
- DisposePtr((Ptr) pIOPb);
- FWDebugStr("\pOut of memory, SetupIOQ");
- err = memFullErr;
- break;
- }
-
- Enqueue((QElemPtr) pIOPb, &gFWControlQHdr);
- }
-
- return err;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // InitNotification
- //
- // Set up our notify routine
- //zzz must deallocate pNotifyRec.
- //
- static OSErr InitNotification (void)
- {
- NMRecPtr pNotifyRec;
- OSErr err = noErr;
-
- pNotifyRec = (NMRecPtr) NewPtrClear(sizeof(NMRec));
- if (pNotifyRec == nil)
- err = memFullErr;
- else {
- pNotifyRec->qType = nmType;
- pNotifyRec->nmResp = NewNMProc(HandleNotifyResponse);
- pNotifyRec->nmMark = 1;
- pNotifyRec->nmRefCon = kNoNotificationPosted;
- UpdateNotification(pNotifyRec);
- }
- gpFWXAppData->pNotifyRec = pNotifyRec;
- return err;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // UpdateNotification
- //
- // Setup notification parameters according to prefs
- //zzz must deallocate nmStr, hRes?
- //
- void UpdateNotification (
- NMRecPtr pNMRec)
- {
- Handle hRes;
- SInt16 curAttributes;
- OSErr err;
- SInt8 notifyPrefs = gpFWXAppData->fwixPrefs;
-
- if (notifyPrefs & kNotifyFlash)
- {
- if (pNMRec->nmIcon == nil)
- {
- // get an icon to flash
- err = GetIconSuite(&hRes, kAppIconSuiteID, svAllSmallData);
- if ((err == noErr) && (hRes != nil))
- {
- curAttributes = GetResAttrs(hRes);
- if (curAttributes & resPurgeable)
- HNoPurge(hRes);
- pNMRec->nmIcon = hRes;
- }
- else
- pNMRec->nmIcon = nil;
- }
- }
- else if (pNMRec->nmIcon != nil)
- {
- DisposeIconSuite(pNMRec->nmIcon, false);
- pNMRec->nmIcon = nil;
- }
-
- /*
- *** JKl - do the dialog ourselves so we can continue processing in the background,
- *** this code uses notification manager for dialog.
- *** Need to post dialog needed message and display dialog if we are in background.
- *** HandleReceive displays the dialog once receive is complete.
- if (notifyPrefs & kNotifyAlert)
- {
- if (pNMRec->nmStr == nil)
- {
- // get an alert string
- hRes = GetResource('STR ', kNotifyString);
- pNMRec->nmStr = (StringPtr) NewPtr(**hRes + 1);
- if (pNMRec->nmStr != nil)
- BlockMove(*hRes, pNMRec->nmStr, **hRes + 1);
- ReleaseResource(hRes);
- }
- }
- else if (pNMRec->nmStr != nil)
- {
- DisposePtr((Ptr) pNMRec->nmStr);
- pNMRec->nmStr = nil;
- }
- */
- /*
- *** JKL - just play the sound our selves, this code uses the notification manager to make the sound
- *** HandleReceive now plays the sound once receive is complete
- if (notifyPrefs & kNotifySound)
- {
- if (pNMRec->nmSound == nil)
- {
- // get the sound to play
- hRes = GetNamedResource('snd ', gpFWXAppData->fwixNotifySound);
- if (hRes != nil)
- {
- curAttributes = GetResAttrs(hRes);
- if (curAttributes & resPurgeable)
- HNoPurge(hRes);
- pNMRec->nmSound = hRes;
- }
- else
- pNMRec->nmSound = (Handle) -1; // at least play the system alert
- }
- } else if (pNMRec->nmSound != nil) {
- ReleaseResource(pNMRec->nmSound);
- pNMRec->nmSound = nil;
- }
- */
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // HandleNotifyResponse
- //
- // Handle notification manager response
- //
- pascal void HandleNotifyResponse (
- NMRecPtr pNMRequest)
- {
- if (gpFWXAppData->inForeground) {
- NMRemove(pNMRequest);
- pNMRequest->nmRefCon = kNoNotificationPosted;
- }
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // FWXInit
- //
- // Initialization routine for FWX
- //
- static OSErr FWXInit(void)
- {
- Handle hString;
- SInt16 curResFile;
- SInt16 itemHit;
- OSErr err = noErr;
-
- // Create global data record
- gpFWXAppData = (FWXAppDataPtr)
- NewPtrClear(sizeof (FWXAppData));
- if (gpFWXAppData == nil)
- err = memFullErr;
-
- if (err == noErr) {
- gpFWXAppData->quitFlag = false;
- gpFWXAppData->inForeground = true;
- gpFWXAppData->fwxClientID = kInvalidFWXClientID;
- gpFWXAppData->pSenderWindow = nil;
-
- gCheckingTransfer = false;
- gSendingFile = false;
- gSendingDataFork = false;
- gSendingResFork = false;
- gForkComplete = true;
- gForkWriteComplete = true;
- gSendError = (FWXNodeID) 0;
- gReceiveError = (FWXNodeID) 0;
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- // Make sure there is a sharing setup macintosh name
- curResFile = CurResFile();
- UseResFile(kSystemResFile);
- hString = Get1Resource('STR ', kNetworkNameID);
- UseResFile(curResFile);
- if ((hString == nil) || (**hString == 0))
- {
- // no name or string is empty
- itemHit = HandleStopAlert(kNoNameAlertID);
- if (itemHit == kOKButton)
- OpenSharingSetup();
- ExitToShell();
- }
-
- if (err == noErr)
- err = SetupIOQueue();
- if (err == noErr)
- err = InstallAppleEventHandlers();
- if (err == noErr)
- err = InstallCompletionRoutineProcs();
- if (err == noErr)
- err = CreateMenus();
- if (err == noErr)
- err = InitPrefs();
- if (err == noErr)
- err = NewSenderWindow();
- if (err == noErr)
- err = InstallDragHandlers();
- if (err == noErr)
- err = SetupFWXNode();
- if (err == noErr)
- err = InitNotification();
- }
- return err;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // FWXDispose
- //
- // Disposal routine for FWX
- //
- static OSErr FWXDispose(void)
- {
- IOParamPtr pIOPb;
- OSErr err = noErr;
-
- // dispose of file write parameter blocks and write queue
- //zzz must ensure that none are still pending
-
- // dispose of FW control parameter blocks and write queue
- //zzz must ensure that none are still pending
- if (queuesInitialized) {
- pIOPb = (IOParamPtr) gFWControlQHdr.qHead;
- while (pIOPb) {
- Dequeue ((QElemPtr) pIOPb, &gFWControlQHdr);
- UnholdMemory (pIOPb->ioBuffer, kFWControlParamBufferSize);
- DisposePtr (pIOPb->ioBuffer);
- UnholdMemory ((Ptr) pIOPb, sizeof(IOParam));
- DisposePtr ((Ptr) pIOPb);
-
- pIOPb = (IOParamPtr) gFWControlQHdr.qHead;
- }
- }
-
- // dispose of file read parameter blocks and write queue
- //zzz must ensure that none are still pending
- if (queuesInitialized) {
- pIOPb = (IOParamPtr) gFileReadQHdr.qHead;
- while (pIOPb) {
- Dequeue ((QElemPtr) pIOPb, &gFileReadQHdr);
- UnholdMemory (pIOPb->ioBuffer, kFileReadBufSize);
- DisposePtr (pIOPb->ioBuffer);
- UnholdMemory ((Ptr) pIOPb, sizeof(IOParam));
- DisposePtr ((Ptr) pIOPb);
-
- pIOPb = (IOParamPtr) gFileReadQHdr.qHead;
- }
- }
-
- if (gpFWXAppData != nil) {
- // Unregister as a client of FWiX.
- if (gpFWXAppData->fwxClientID != (FWXClientID) kInvalidFWXClientID)
- UnregisterFWXClientApplication (gpFWXAppData->fwxClientID);
-
- // Deallocate our globals.
- DisposePtr ((Ptr) gpFWXAppData);
- gpFWXAppData = nil;
- }
-
- return err;
- }
-